[英]Nested call to function in Javascript
Intellij IDEA shows a warning when ever I write Javascript like this: 当我写这样的Javascript时,Intellij IDEA会显示警告:
someFunction(someOtherFunction());
But the explanation is not really helpful: 但解释并不是很有用:
"This inspection reports any Javascript function calls used as arguments to another function call."
“此检查报告任何Javascript函数调用用作另一个函数调用的参数。”
This is something I do frequently, so what's the potential trap hiding there worth warning of? 这是我经常做的事情,那么隐藏在哪里的潜在陷阱值得警告? Or if it's just some coding convention, what's the reason for it?
或者,如果它只是一些编码约定,它的原因是什么?
It is a warning because most of the time, you want to pass a function reference as an argument. 这是一个警告,因为大多数情况下,您希望将函数引用作为参数传递。 It is mostly used as a callback:
它主要用作回调:
someFunction(someOtherFunction);
function someFunction(fn){
fn.call();
}
In that example, someOtherFunction()
instead of someOtherFunction
would not work as expect (unless someOtherFunction
returns a function itself). 在该示例中,
someOtherFunction()
而不是someOtherFunction
将无法按预期工作(除非someOtherFunction
返回一个函数本身)。
someFunction(someOtherFunction());
work more like a getter. 工作更像是一个吸气剂。
someFunction(someOtherFunction());
function someFunction(int){
alert(int === 1);//True;
}
function someOtherFunction(){
return 1;
}
It gives a warning because it is a common mistake for new developers. 它给出了一个警告,因为它是新开发人员的常见错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.