当我像这样编写 Javascript 时,Intellij IDEA 会显示警告:
someFunction(someOtherFunction());
但是解释并没有多大帮助:
"This inspection reports any Javascript function calls used as arguments to another function call."
这是我经常做的事情,那么其中隐藏着哪些值得警惕的潜在陷阱呢?或者,如果这只是一些编码约定,那么原因是什么?
最佳答案
这是一个警告,因为大多数时候,您希望将函数引用作为参数传递。它主要用作回调:
someFunction(someOtherFunction);
function someFunction(fn){
fn.call();
}
在该示例中,someOtherFunction()
而不是 someOtherFunction
将无法按预期工作(除非 someOtherFunction
返回一个函数本身)。
someFunction(someOtherFunction());
更像一个 setter/getter 。
someFunction(someOtherFunction());
function someFunction(int){
alert(int === 1);//True;
}
function someOtherFunction(){
return 1;
}
它会发出警告,因为这是新开发人员的常见错误。
关于javascript - Javascript 中函数的嵌套调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22562566/