简体   繁体   English

Function 名称被该函数的参数名称遮蔽

[英]Function name shadowed by that function's parameter name

Warning: The 5th edition of ECMAScript (ES5) forbids use of arguments.callee() in strict mode.警告:ECMAScript (ES5) 第 5 版禁止在严格模式下使用 arguments.callee()。 Avoid using arguments.callee() by either giving function expressions a name or use a function declaration where a function must call itself.避免使用 arguments.callee() 给 function 表达式一个名称或使用 function 声明,其中 function 必须调用自身。 [ MDN ] [ MDN ]

How does one reference the o function within itself in the following situation?在以下情况下,如何在自身内部引用o function

function o(o) { o(); }
o('not a function'); // TypeError: o is not a function

EDIT:编辑:

Why not rename the parameter/function?为什么不重命名参数/函数?

  1. I am debugging thousands of files of obfuscated JavaScript code.我正在调试数千个混淆的 JavaScript 代码文件。
  2. My own personal opinion is that this problem should be avoidable via reflection alone, not refactoring.我个人的看法是,这个问题应该可以通过反思来避免,而不是通过重构。

EDIT:编辑:

In the above situation, I am looking for either a way to reference the function within itself or, alternatively, an explanation or a reference to a credible source as to why the parameter name shadows the function name .在上述情况下,我正在寻找一种在其自身内部引用 function 的方法,或者寻找一种解释或对可靠来源的引用,说明参数名称为何隐藏 function 名称

Naming a parameter a name that another declared variable has makes it the value in the function. It ignores the other declaration(s) and uses the one in the parameters.将参数命名为另一个已声明变量的名称,使其成为 function 中的值。它会忽略其他声明并使用参数中的声明。 The best thing to do is rename it, but if it is in the global scope, you can access it with window (HTML documents only).最好的办法是重命名它,但如果它在全局 scope 中,则可以使用window (仅限 HTML 文档)访问它。 Just type window.functionName() and it will reference correctly.只需键入window.functionName()正确引用。 This only works if it was defined in the global scope with the var keyword!这仅在使用var关键字在全局 scope 中定义时才有效!

 //global scope var a = 1 function b(a) { console.log(a) console.log(window.a) } b(2) //2 and then 1 function o(o) { console.log(++a) if(a < 5) window.o() //calls o() from global scope } o()

Parameter o of o function is not a string. o function 的参数 o 不是字符串。 It should be a reference to any function or function name.它应该是对任何 function 或 function 名称的引用。

function a() { console.log('a function'); }
function o(o) { console.log('o function'); o(); }
o(a); 
// o function
// a function

Used shift-refactor to implement the obvious ( rename ) solution, as I did not find another one:使用shift-refactor来实现明显的( rename )解决方案,因为我没有找到另一个解决方案:

$source('FunctionDeclaration,FunctionExpression').forEach(fd => {
  if(fd.name)
  {
    for (let param of fd.params.items) {
      if (param.name === fd.name.name) {
        $source(param).rename('randomParameterName');
        break;
      }
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM