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