I'm trying to iterate over all methods in a JavaScript pseudoclass and can easily tell if something is a method or not with (obj.member instanceof Function), however I'm trying to include methods that may be hidden from a for...in loop via defineProperty with an enumerable flag set to false - how do I iterate all members of a pseudoclass, regardless of the enumerable value?
How do you iterate over all methods in a JavaScript pseudoclass, regardless of whether or not they are marked enumerable?
Asked Answered
You can always use Object.getOwnPropertyNames
, which will include non-enumerable properties as well. However, this will not include properties from prototypes, so if you are asking about "pseudoclass instances" you might need to loop the prototype chain with Object.getPrototypeOf
.
Correction, in 8 minutes you can have some karma - damned site think's I'm a bot or something. –
Nuptials
what if, in inspection, I can see a property but it doesn't apear under
getOwnPropertyNames
nor keys
? Example: typeof obj['stuff'] == 'function'; Object.getOwnPropertyNames(this).indexOf('stuff') == -1
. This happened on an ES6 class, babelified. –
Caulicle @igorsantos07: as the answer says, it's likely an inherited property. Try
Object.getOwnPropertyNames(Object.getPrototypeOf(this))
, or use a complete loop. –
Apiary whoa. that's confusing. it seems from the object instance I need to use getPrototypeOf to get to the actual defined class. might be some sort of effect from using Babel to transpile code :| –
Caulicle
@igorsantos07: No, that's how prototypes worked since the beginning, even without ES6 or Babel. –
Apiary
so getOwnPropertyNames won't include properties from prototypes nor properties defined by its own class, right? –
Caulicle
@Caulicle I'm not sure what you mean by "its own class". Every instance inherits the shared class methods from the prototype object. Only instance-specific data is stored in own properties. –
Apiary
Let us continue this discussion in chat. –
Caulicle
The following link might be relevant here. –
Brabant
© 2022 - 2024 — McMap. All rights reserved.