4

How I can get the Javascript's Global Object's (window object) properties apart from default properties, So that I would know that for a particular application, which variables have became (poluted the global scope) the global variables.

Object.getOwnPropertyNames(window); 

This would give me all the properties of window object. So what I am interested in is filtering these properties and get only the properties that were not there initially in window object, in short getting the non-default properties.

Well of course, I can first get the properties of window object, store it in an array and then compare and filter with new (polluted) window object, but I wanted to know that is there any direct API (a method) that can give me the non-default properties.

vinayakj
  • 5,591
  • 3
  • 28
  • 48
  • 1
    Interesting. The most difficult thing would be to separate default from user defined variables. You can filter out all methods by iterating and checking whether it is a function or not. But for properties, everything looks the same. Also, it varies based on libraries included like jquery and browser. –  Jul 18 '14 at 08:31
  • 1
    You also need to avoid non–enumerable properties, which may also vary by browser. Simpler to adopt a naming scheme that is unlikely to clash and keep globals to a minimum (within reason of course). Formal properties of the [*global object*](http://ecma-international.org/ecma-262/5.1/#sec-15.1) are in ECMA-262, and for the [*window object*](http://www.w3.org/html/wg/drafts/html/master/browsers.html#the-window-object) in the HTML specification. But there are other, implementation dependent properties (including potentially all element IDs and names). – RobG Jul 18 '14 at 08:33

0 Answers0