js学习小结(十五)2014.7.22-Javascript中的IIFE--(Immediately Invoked function Expression)

编程知识 更新时间:2023-05-02 18:59:55

  if declaring  a function like this,and then invoking it immediately,

function foo(){console.log("it is foo");}();
 it will throw a error,
Uncaught SyntaxError: Unexpected token )

but if you just a arguement in the end of the paren,like this

function foo(){console.log("it si foo");}(1);

ok.it won`tl occures any error,but in the same time,the funciton won`t be executed either.because it just equivlent to this:

function foo(){console.log("it si foo");}



(1);

a function declaration followed by a unrelated expression,so you can get it that the function not be executed.

so what does it matter?ok.if you just want to declare a function,and want to execute it right now,you can`t tolerate one second to wait.you shuold know how to implement that.ok,we define the IIFE(Immediately Invoked Function Expression);


http://benalman/news/2010/11/immediately-invoked-function-expression/





更多推荐

js学习小结(十五)2014.7.22-Javascript中的IIFE--(Immediately Invoked function Expression)

本文发布于:2023-04-28 04:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/9feccf06f2d319c3611fe2183d22ecb8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小结   js   十五   Javascript   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 107570文章数
  • 27224阅读数
  • 0评论数