Reputation: 6739
I've few questions after reading this: hasOwnProperty vs propertyIsEnumerable
Can I say all methods in an object are not enumerable? If not, can you give an example of a method which is enumerable? How to create a enumerable method?
Upvotes: 0
Views: 771
Reputation: 665276
I've few questions after reading this: hasOwnProperty vs propertyIsEnumerable
You seem to have only read the question :-)
Can I say all methods in an object are not enumerable?
First of all, there are no "methods" in JavaScript. An object just can have a property that is a function. Or it can inherit one from its prototype object.
If not, can you give an example of a method which is enumerable?
If you take the example function from the linked question, and loop over an instance you will get all enumberable properties, both inherited and direct:
> for (var prop in new f) console.log(prop)
a
b
c
d
e
g
It did not log the nonenumerable properties that were inherited from Object.prototype
, like toString
or isPrototypeOf
.
So why does isEnumberable
return false? That is explained well in the answers: The function only takes own properties into account, not the ones inherited from the prototype.
How to create a enumerable method?
The question should rather be "How to create an non-enumerable method?". For that task use Object.defineProperty
.
Every other property that is created by simple assignment is enumerable by default. This applies both on normal properties and those inherited from their prototypes.
Upvotes: 0
Reputation: 5792
As per default all methods you add to an object or it's prototype chain are enumerable.
If you look into any debug tool such as firebug or webkit inspector you'll notice some properties which won't show up in any for in
loop.
Such properties are for example prototype
, __proto__
, isPrototypeOf
and so on.
Upvotes: 1