I'm trying to use defineProperty to made attributes not appear in for...in cycle, but it doesn't work. Is this code correct?
function Item() {
this.enumerable = "enum";
this.nonEnum = "noEnum";
}
Object.defineProperty(Item, "nonEnum", { enumerable: false });
var test = new Item();
for (var tmp in test){
console.log(tmp);
}
I'm trying to use defineProperty to made attributes not appear in for...in cycle, but it doesn't work. Is this code correct?
function Item() {
this.enumerable = "enum";
this.nonEnum = "noEnum";
}
Object.defineProperty(Item, "nonEnum", { enumerable: false });
var test = new Item();
for (var tmp in test){
console.log(tmp);
}
Share
Improve this question
edited Apr 22, 2012 at 16:25
kapa
78.7k21 gold badges165 silver badges178 bronze badges
asked Apr 22, 2012 at 16:13
NaigelNaigel
9,64417 gold badges71 silver badges109 bronze badges
1 Answer
Reset to default 18Item
does not have a property named nonEnum
(check it out). It is a (constructor) function that will create an object that has a property called nonEnum
.
So this one would work:
var test = new Item();
Object.defineProperty(test, "nonEnum", { enumerable: false });
You could also write this function like this:
function Item() {
this.enumerable = "enum";
Object.defineProperty(this, "nonEnum", {
enumerable: false,
value: 'noEnum'
});
}
jsFiddle Demo