I created this object and it’s properties:
var obj = {};
Object.defineProperty( obj, "value", {
value: true,
writable: false,
enumerable: true,
configurable: true
});
var name = "John";
Object.defineProperty( obj, "name", {
get: function(){ return name; },
set: function(value){ name = value; }
});
So then I call a for loop on them:
for ( var prop in obj ) {
console.log( prop );
}
Which according to my tutorial, should produce the following results:
value
name
But instead it only displays value. Why is name not showing up?
The default value for
enumerableindefinePropertyisfalse; non-enumerable properties do not show up infor…inloops. (That’s the whole point of theenumerableflag.) If you addenumerable:trueinto your second definition also, it will ‘fix’ it.See some docs.