I’m writing a little javascript library: I’ve defined an Item object, then added a function with Item.prototype.addNumber and finally I set it as not enumerable, but if I try to log Item’s method using for...in loop the function still appears.
This is my code, Am I doing something wrong? (tested on Chrome 18 and Firefox 11)
function Item() {
...
}
Item.prototype.addString= function() {
...
}
Object.defineProperty(Item, "addString", { enumerable: false });
You’re defining the property on
Iteminstead of onItem.prototype.If you used
Object.definePropertyto initially addaddStringtoItem.prototype, then you could explicitly (or implicitly) set the property descriptors at the same time…This will implicitly set
enumerable:false, as well asfalseforconfigurableandwritable.Or if you wanted only
enumerableto befalse, you could do this…