[英]my anonymous function in javascript doesn't work
On about line 18, make the anonymous function with the code 'window.didExecute = true' execute. 在大约第18行,执行带有代码“ window.didExecute = true”的匿名函数。
var anonymousFunction = function(){};
(function(){window.didExecute=true;})
doesn't work, why? 不起作用,为什么?
Because the function is never executed. 因为该函数永远不会执行。 Use an immediately invoked function expression:
使用立即调用的函数表达式:
(function(){window.didExecute=true;})();
The ()
at the end is what actually makes it a function call, resulting in the body of the function executing. 最后的
()
实际上使它成为函数调用,从而导致函数主体执行。
If you weren't using anonymous functions, your code would be the same as doing: 如果您没有使用匿名函数,则您的代码将与执行以下操作相同:
function foo() {
window.didExecute = true;
}
Then never calling foo()
. 然后永远不要调用
foo()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.