[英]In JavaScript, why can't I immediately invoke function declarations?
Only functions expressions can be immediately invoked: 仅函数表达式可以立即被调用:
(function () {
var x = "Hello!!"; // I will invoke myself
})();
But not function declarations? 但是不是函数声明吗? Is this because function declarations are hoisted and already execute immediately?
这是因为函数声明被吊起并已经立即执行吗?
EDIT: Resources I'm referencing 编辑:我引用的资源
http://benalman.com/news/2010/11/immediately-invoked-function-expression/ http://benalman.com/news/2010/11/immediately-invoked-function-expression/
http://markdalgleish.com/presentations/gettingclosure/ http://markdalgleish.com/presentations/gettingclosure/
"...While parens placed after an expression indicate that the expression is a function to be invoked, parens placed after a statement are totally separate from the preceding statment, and are simply a grouping operator (used as a means to control precedence of evaluation)." “ ...放在表达式后面的括号表示该表达式是要调用的函数,而放在语句后面的括号与前面的语句完全分开,它们只是一个分组运算符(用作控制评估优先级的一种手段)“。
// While this function declaration is now syntactically valid, it's still
// a statement, and the following set of parens is invalid because the
// grouping operator needs to contain an expression.
function foo(){ /* code */ }(); // SyntaxError: Unexpected token )
// Now, if you put an expression in the parens, no exception is thrown...
// but the function isn't executed either, because this:
function foo(){ /* code */ }( 1 );
// Is really just equivalent to this, a function declaration followed by a
// completely unrelated expression:
function foo(){ /* code */ }
( 1 );
Therefore, you need to write the function as 因此,您需要将函数编写为
(function doSomething() {})();
as this tells the parser to evaluate it as a function expression as opposed to a function declaration. 因为这告诉解析器将其评估为函数表达式,而不是函数声明。 And all you're doing then is immediately invoking the expression.
然后,您要做的就是立即调用该表达式。
To clear the confusion 为了消除混乱
What is Function declaration 什么是函数声明
// this is function declaration
function foo(){
// code here
}
OR 要么
//this is ok, but without name, how would you refer and use it
function (){
// code here
}
to call it immediately you do this 立即调用它
function foo(){
// code here
}()
What is Function expression 什么是函数表达式
// this is a function expression
var a = function foo(){
// code here
};
or 要么
var a = function (){
// code here
};
in the second case you have created a anonymous function.you still have a reference to the function through the variable a
.so you could do a()
. 在第二种情况下,您创建了一个匿名函数。您仍然可以通过变量
a
对该函数进行引用,因此可以执行a()
。
invoking function expression 调用函数表达式
var a = (function (){
// code here
}());
the variable a is stored with the result of the function(if you return from the function) and loses the reference to the function. 变量a与函数的结果一起存储(如果您从函数返回),并且丢失对该函数的引用。
In both the cases you can immediately invoke a function but the outcome differs as pointed above. 在这两种情况下,您都可以立即调用函数,但是结果与上面指出的不同。
Not sure what you mean exactly - if you run a function declaration in the way you have shown it will still execute immediately 不确定您的意思是什么-如果您按照显示的方式运行函数声明,它将仍然立即执行
(function declaredFn(){ document.getElementById('result').innerHTML='executed'; }());
<div id="result"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.