CodeQL 文档

在严格模式下使用调用堆栈自省

ID: js/strict-mode-call-stack-introspection
Kind: problem
Security severity: 
Severity: error
Precision: high
Tags:
   - correctness
   - language-features
Query suites:
   - javascript-security-and-quality.qls

单击以查看 CodeQL 存储库中的查询

参数对象的calleecaller属性以及函数对象的callerarguments属性在严格模式代码中不可用,任何尝试访问它们的尝试都会导致运行时错误。

建议

不要使用arguments.callee,您可以通过名称引用封闭函数(如果它是匿名函数表达式,则可能需要先为其命名)。其他属性的使用通常可以通过重构程序来消除。

示例

在以下示例中,arguments.callee用于递归调用封闭函数,该函数是匿名的。

var o = {
  A: function(x) {
       'use strict';
       if (!(this instanceof arguments.callee))
         return new arguments.callee(x);
       this.x = x;
     }
};

为了避免这种情况,可以为该函数命名并使用该名称进行引用

var o = {
  A: function A(x) {
       'use strict';
       if (!(this instanceof A))
         return new A(x);
       this.x = x;
     }
};

参考

  • ©GitHub, Inc.
  • 条款
  • 隐私