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.