Is there a way for me to loop over a Javascript Object's built-in properties?
for...in gets me close to where I want to go, but "A for...in loop does not iterate over built-in properties."
Is there a way for me to loop over a Javascript Object's built-in properties?
for...in gets me close to where I want to go, but "A for...in loop does not iterate over built-in properties."
No you can't list object's built in properties. But you can refer to the implementer's reference. For example, to know all the methods and properties of Math object implemented on firefox, you'll use Firefox's Javascript Math Reference: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math
When you say "built in properties", which set of properties are you exactly talking about ?
From Douglas Crockford's 'JavaScript - The Good Parts' :
This will work with JSON. It hasn't been tested much:
I realize this question is three years old, but now, with ES5, it is possible:
>>> Object.getOwnPropertyNames(Object)
["prototype", "getPrototypeOf", "getOwnPropertyDescriptor", "keys", "defineProperty", "defineProperties", "create", "getOwnPropertyNames", "isExtensible", "preventExtensions", "freeze", "isFrozen", "seal", "isSealed", "length", "arity", "name", "arguments", "caller"]
The answer is no. You can't enumerate properties that aren't enumerable. There are however at least two ways around this.
The first is to generate all possible combination of characters to use as test property names (think: a, b, c, ... aa, ab, ac, ad, ... ). Given that the standards community is famous for coming up with really long method names (getElementsByTagNames, propertyIsEnumerable) this method will require some patience. :-)
Another approach is to test for known native properties from some predefined list.
For example: For an
array
you would test for all known native properties ofFunction.prototype
:...and things inherited from
Object.prototype
:...and things inherited from
Array
:..and finally, and optionally, every enumerable property of the object you are testing:
You'll then be able to test every one of those to see if they are present on the object instance.
This won't reveal unknown non-enumerable members (undocumented, or set by other scripts), but will allow you to list what native properties are available.
I found the information on native
Array
properties on Mozilla Developer Center and MSDN.