That is, I want to detect all the global properties added by libraries such as $,
_,
Backbone
, etc.
Asked
Active
Viewed 350 times
0
1 Answers
0
The only way of doing this that I know about, is unfortunately by storing properties of window
when it's in its initial state and comparing them to the properties of window
at the time when you are interested in finding the user-defined ones.
It's a dirty "hack" with awful computational complexity of O(n2), but I don't think there is other way.
// In the very beginning, before anything modifying window runs
const initialWindowProperties = Object.getOwnPropertyNames(window);
// Later
window.$ = undefined; // Dummy window property addition
const getUserDefinedWindowProperties = () => {
const windowProperties = Object.getOwnPropertyNames(window);
return windowProperties.filter(
(property) => !initialWindowProperties.includes(property)
);
}
console.log(getUserDefinedWindowProperties());
/* Output console formatting */
.as-console-wrapper { max-height: auto; top: 0; }

Przemek
- 3,855
- 2
- 25
- 33