Get All User Defined Window Properties

Get all user defined window properties?

You would need to do the work for yourself. Read in all properties, on the first possible time you can. From that point on, you can compare the property list with your static one.

var globalProps = [ ];

function readGlobalProps() {
globalProps = Object.getOwnPropertyNames( window );
}

function findNewEntries() {
var currentPropList = Object.getOwnPropertyNames( window );

return currentPropList.filter( findDuplicate );

function findDuplicate( propName ) {
return globalProps.indexOf( propName ) === -1;
}
}

So now, we could go like

// on init
readGlobalProps(); // store current properties on global object

and later

window.foobar = 42;

findNewEntries(); // returns an array of new properties, in this case ['foobar']

Of course, the caveat here is, that you can only "freeze" the global property list at the time where your script is able to call it the earliest time.

Any way to list all global (window) variables that are user-added?

// setTimeout is needed and is being used here just if the property adding and the checking will be done in sequence. To avoid getting the same reference values on the current time lapse. Check the answer code
// Get current window Properties;var windowInitProperties = Object.getOwnPropertyNames(window);
setTimeout(() => window.newProperty = 'newProperty', 0)
setTimeout(() => { // List new Properties; console.log( Object.getOwnPropertyNames(window).filter(property => { return windowInitProperties.indexOf(property) < 0; }) );}, 0)

List all properties of window object?

In Firefox, it seems to be the behavior of elements that their global object is not added unless explicitly requested as a global variable or property. Perhaps Firefox lazy loads them into the environment so that they don't consume memory unless they're actually needed.

It seems that they do not show up when simply requesting the keys of the global object via Object.getOwnPropertyNames unless they've first been explicitly referenced as described above.

http://jsfiddle.net/mBAHm/

How can I list all the properties of Math object?

Not all object properties are iterable. You'll only get iterable properties in a for..in loop.

Since most properties of window (which happens to be the global object) are user-defined global variables, they are enumerable.

In modern JavaScript engines you can use Object.getOwnPropertyNames(obj) to get all properties, both enumerable and non-enumberable:

>>> Object.getOwnPropertyNames(Math)
["toSource", "abs", "acos", "asin", "atan", "atan2", "ceil", "cos", "exp", "floor", "log", "max", "min", "pow", "random", "round", "sin", "sqrt", "tan", "E", "LOG2E", "LOG10E", "LN2", "LN10", "PI", "SQRT2", "SQRT1_2"]

See Is it possible to get the non-enumerable inherited property names of an object? for more details.

How do you explicitly set a new property on `window` in TypeScript?

I just found the answer to this in another Stack Overflow question's answer.

declare global {
interface Window { MyNamespace: any; }
}

window.MyNamespace = window.MyNamespace || {};

Basically, you need to extend the existing window interface to tell it about your new property.

View list of all JavaScript variables in Google Chrome Console

Is this the kind of output you're looking for?

for(var b in window) { 
if(window.hasOwnProperty(b)) console.log(b);
}

This will list everything available on the window object (all the functions and variables, e.g., $ and jQuery on this page, etc.). Though, this is quite a list; not sure how helpful it is...

Otherwise just do window and start going down its tree:

window

This will give you DOMWindow, an expandable/explorable object.

Fetching all (javascript) global variables in a page

Something like this:

function getGlobalProperties(prefix) {
var keyValues = [], global = window; // window for browser environments
for (var prop in global) {
if (prop.indexOf(prefix) == 0) // check the prefix
keyValues.push(prop + "=" + global[prop]);
}
return keyValues.join('&'); // build the string
}

A test usage:

var xxx_foo = "foo";
xxx_bar = "bar";
window.xxx_baz = "baz";

var test = getGlobalProperties('xxx_');
// test contains "xxx_baz=baz&xxx_bar=bar&xxx_foo=foo"


Related Topics



Leave a reply



Submit