Friday, 22 March 2019

Add a property to a node file

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 only solution I found to this was replacing global with this but then I have to also type this to call the property. Finally since the property is defined like this, eslint is warning me that the property does not exist.

So is there a way to create a property that

  1. Is not fully global
  2. Can be accessed without stating the owner object
  3. Can be recognized by eslint.


from Add a property to a node file

No comments:

Post a Comment