也就是说,我想检测库添加的所有全局属性,例如$,
_,
Backbone
等。
答案 0 :(得分:0)
不幸的是,我知道这样做的唯一方法就是window
处于初始状态时的属性,并在感兴趣时将它们与window
的属性进行比较。找到用户定义的那些。
这是一个肮脏的“黑客”,计算复杂度很高(O
// 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; }