Skip to content

Instantly share code, notes, and snippets.

@JonathanGawrych
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save JonathanGawrych/c49576952a533c13dc6b to your computer and use it in GitHub Desktop.

Select an option

Save JonathanGawrych/c49576952a533c13dc6b to your computer and use it in GitHub Desktop.
determine if an object is a global object/constructor (based if it's non-enumerable properties of window)
// get all native objects key names (include non-enumerable properties)
var windowItems = Object.getOwnPropertyNames(window);
// exclude deprecated browser items to avoid console warnings
// the tradeoff is 'isOnWindow' will incorrect report false for these items
var deprecatedItems = ['webkitIDBTransaction',
'webkitIDBRequest',
'webkitIDBObjectStore',
'webkitIDBKeyRange',
'webkitIDBIndex',
'webkitIDBFactory',
'webkitIDBDatabase',
'webkitIDBCursor',
'webkitIndexedDB',
'webkitStorageInfo'
]
for (var i = 0; i < deprecatedItems.length; i++) {
var deprecatedIndex = windowItems.indexOf(deprecatedItems[i]);
if (deprecatedIndex !== -1) {
windowItems.splice(deprecatedIndex, 1);
}
}
// get the values of all the keys
windowItems = windowItems.map(function getValue(key) {
return window[key];
});
// determine if an item is on the global window object (Native constructors are)
function isOnWindow(item) {
return windowItems.indexOf(item) !== -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment