In a project I'm working on, I'm using Object.defineProperty to define getter and setter methods on some properties, in order to handle specific cases of parsing data. For example:
module.exports = function() {
Object.defineProperty(this.body, 'parsed', {
get: function() {
...
switch(this.type) { // Undefined
}
});
if (this.type) { // Defined correctly
...
}
}
This would work fine if I was setting this.type within the object instantiation function (i.e. I call var foo = new bar()). However, I set this.type using Object.defineProperty on the module.exports.prototype:
Object.defineProperty(module.exports.prototype, 'type', {
get: function() {
...
if (this._type) {
return this._type;
}
}
});
In this case, I know that this._type is set, so this.type should return the expected value, but when I attempt to switch it in the first block of code, this.type is undefined. Strangely, when I call it from outside of the Object.defineProperty function, it is defined and returns the correct type.
Is there any way I can access this.type from within the original Object.defineProperty call?