Last active
August 29, 2015 14:07
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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