in Education by
Is it possible to add a property (with get and set method) to the scope of a file without making it global? (Similar to how let or const would work for a variable declaration) This is the code I've written so far, It can add a property to the global scope. var propertyValue; Object.defineProperty(global, "PropertyValue", { get: function () { return propertyValue; }, set: function (value) { propertyValue = value; } }); console.log(PropertyValue); Is it possible to make the property only visible to just the file it was declared in. The same thing can be done by declaring a variable and adding all properties there. var fileProperties; var propertyValue; Object.defineProperty(fileProperties, "PropertyValue", { get: function () { return propertyValue; }, set: function (value) { propertyValue = value; } }); console.log(fileProperties.PropertyValue); But then I still need to type the name of that variable every time I want to get/set a property. So is there a way to create a property that Is not fully global Can be accessed without stating the owner object Can be recognized by eslint JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
A property should be accessed on some object, the only possibilities for an object to be omitted in JavaScript are global properties and with statement. As the original code shows, this will access a property on global variable, using global variables for local tasks is a bad practice, Using global variables for local tasks is a bad practice: Object.defineProperty(global, "PropertyValue", {...}); console.log(PropertyValue); Another way is to use with statement, which is deprecated and won't work in strict mode: Object.defineProperty(someObject, "PropertyValue", {...}); with (someObject) { console.log(PropertyValue); } In Node, a script is evaluated in the scope of module wrapper function, this.PropertyValue refers to module.exports.PropertyValue in module scope. A property can be defined on export object explicitly: let propertyValue; Object.defineProperty(exports, "PropertyValue", { get: function () { return propertyValue; }, set: function (value) { propertyValue = value; } }); console.log(exports.PropertyValue); PropertyValue will be available to other modules when this module is imported. There are usually no good reasons to enforce encapsulation to the point where it starts to make developer's life harder. If PropertyValue isn't intended to be used outside the module, usually it's enough to use Hungarian notation and underscore internal/private property: Object.defineProperty(exports, "_PropertyValue", { ... }); This way it's still available for testing.

Related questions

0 votes
    here is my code. var http=require("http"); var fs = require("fs"); var express = require(" ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    I am trying to create a simple node.js app on heroku. Here is app.js: console.log("Starting App" ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I have a problem, I want to move value of Object[0] nazwa, which is a result of query (let's ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have a problem, I want to move value of Object[0] nazwa, which is a result of query (let's ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I am using the electron-quick-start app with a windows machine. I have added logging statements in the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    I am using the electron-quick-start app with a windows machine. I have added logging statements in the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am using react FineUploaderS3, with params: this.myUploader = new FineUploaderS3({ options: { request: { ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I have a particular context in which one data are transformed a lot to get transferred across network. At ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    When I run a new page, I must specify size of the viewport using the setViewport function: await page ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I want to build my js/css code, write it on disk and serve it using webpack-dev-server in a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I copied and pasted a for-await-of example from MDN and still get an error telling me await is a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I copied and pasted a for-await-of example from MDN and still get an error telling me await is a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I have been implementing a Next.js app for a side project of mine. It is a basic brochure-style ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I created a REST api and want to validate the body and the params before calling the controller logic. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I'm trying to use babel to run my NodeJS program, which includes ES6 syntax and exports from the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 13, 2022 in Education by JackTerrance
...