I want to "reset" the getter/setter after I changed them using "defineProperty".
The output of the example should be 1 -> nope -> 1, but I just get "undefined".
How can I clear the custom getter, so I it will go with the native default getter?
var obj = {
a: '1',
b: '2'
};
console.log(obj.a);
Object.defineProperty(obj, 'a', {
get: function(){ return 'nope'; },
configurable: true
});
console.log(obj.a);
Object.defineProperty(obj, 'a', {
get: undefined,
configurable: true
});
console.log(obj.a);
I don't see how you'd ever get back to your defaults without cloning obj - because that first getter in essence destroys the original value of 'a'; it's the same as saying obj.a = 'nope'
.
Something like this will work:
let obj = {
a: '1',
b: '2'
};
const objOrig = JSON.parse(JSON.stringify(obj))
console.log(obj.a);
Object.defineProperty(obj, 'a', {
get: function(){ return 'nope'; },
configurable: true
});
console.log(obj.a);
obj = JSON.parse(JSON.stringify(objOrig))
console.log(obj.a);
And note if you ever need to delete a getter you can;
delete obj.a
but it won't revert to the original declaration; 'a' is lost at this point.