Is there a method in which I can iterate through all objects that exist within an instance of JavaScript, regardless of the JavaScript engine that is being used? I'm not interested in iterating through the DOM of a web page. What I want to know is if there is a way that I can iterate through all standard library objects (Array, Math, Date, etc.) as开发者_Go百科 well as any additional libraries that may be specific to various browsers.
No... and yes. The window
object is the global object in a browser. It contains all the native members of the global scope, plus all the functions and properties provided for the DOM.
Native objects like Math, Array and Date are non-enumerable members of the global object, which means you can't iterate over them using a for...in
loop. That covers the "no" part — there's no way of obtaining these objects through iteration in many browsers.
However, with ECMAScript 5th Edition implementations (IE 9, Chrome 7, Firefox 4) you can use Object.getOwnPropertyNames()
to get an array of property names for a specific object. For instance,
console.log(Object.getOwnPropertyNames(window));
Will give you a list of all the members of the global window
object, including Math, Array, Date, etc.
精彩评论