I would like to set the property x when I first initialise my object, and then call Object.defineProperty. If I try to first access foo.x the console tells me that "value is not defined".
var foo = {x:20};
Object.defineProperty(foo, "x", {
writeable: true,
enumerable: true,
configurable: true,
get: function(){
console.log("Get X value");
return value;
},
set: function(newValue){
console.log("Set X value");
value = newValue;
}
});
console.log(foo.x); // **Uncaught ReferenceError: value is not defined**
foo.x = 1;
console.log(foo.x);
foo.x = 10;
console.log(foo.x);