【问题标题】:Is it possible to detect all non standard window properties? [duplicate]是否可以检测所有非标准窗口属性? [复制]
【发布时间】:2017-09-02 02:24:45
【问题描述】:

即我要检测$,_,Backbone等库添加的所有全局属性

【问题讨论】:

    标签: javascript browser


    【解决方案1】:

    我知道的唯一方法是,不幸的是,在 window 处于初始状态时存储其属性,并将它们与 window 在您有兴趣查找用户时的属性进行比较-定义的。

    这是一个肮脏的“黑客”,计算复杂度为 O(n2),但我认为没有其他方法。

    // 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; }

    【讨论】:

      猜你喜欢
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多