Try and do the following:
for (var key in String.prototype)
console.log(key);
It gives you…nothing (well, unless you defined some foreign stuff yourself.) However, you still have String.prototype.split for example. I tried it on every other native object (Number, Array, Object) for the same result.
The following also “doesn’t work”:
for (var key in Array)
console.log(key);
While there’s Array.isArray for example.
Object.keys(Array.prototype) gives an empty array, and so does Object.keys(Array). However, Object.keys(jQuery) for example provides a giant array, as expected.
So, why can’t we iterate over natives provided by the browser, yet still access them?
From the MDC page for
for..in:The reason is that properties in Javascript are either enumerable or non-enumerable; “enumerable” means you can access the property in a
for..inloop. All built-in properties are non-enumerable.Modern browsers support the
Object.getOwnPropertyNamesmethod: