JS closure anonymous function JavaScript iife (Instant execution method) immediately-invoked function expression

Source: Internet
Author: User

Reference: http://segmentfault.com/a/1190000000348228

http://segmentfault.com/q/1010000000442042

Problem:

(function() {a() {alert ("a");}}) ();
Here (function () {XXX}) (); What do you mean, why it's so good to write?
For:
function foo() {...}     // 这是定义,Declaration;定义只是让解释器知道其存在,但是不会运行。foo(); // 这是语句,Statement;解释器遇到语句是会运行它的。
(1)标准写法 
JS Code
    1. (function (window, document, undefined) {
    2. //   
    3. }) (window, document);


(2)作用域Scope 
JavaScript有function作用域,所以function首先创建一个私有的作用域,在执行的时候都会创建一个执行上下文。
JS Code
    1. (function (window, document, undefined) {
    2. var name = ' Zhang San ';
    3. }) (window, document);
    4. Console.log (name);  //Because the scope is different, the name here is undefined.


调用方法一: 
JS Code
    1. var logmyname = Function (name) {
    2. Console.log (name);
    3. };
    4. Logmyname (' John Doe ');


调用方法二: 
JS Code
    1. var logmyname = (function (name) {
    2. Console.log (name);
    3. }) (' Harry ');


需要注意的是需要用括号把函数内容括起来: 
JS Code
    1. function () {
    2. // ...  
    3. })();


没有括号的话会报语法错: 
JS Code
    1. function () {
    2. // ...  
    3. }();


也可以强制JavaScript识别代码(一般很少这么用): 
JS Code
    1. ! function  ()  {  
    2.   // ...  
    3. } ();   
    4.   
    5. +function  ()  {   
    6.   // ...  
    7. } ();   
    8.   
    9. -function  ()  {  
    10.   // ...  
    11. } ();   
    12.   
    13. ~function  ()  {  
    14.   < span class= "comment" >// ...  
    15. } ();   


比如: 
JS Code
    1. var a = ! function  ()  {  
    2.     return  true;  
    3. } ();   
    4. Console.log (a);  //  print output  false  
    5.   
    6. var b =  (function  ()  {  
    7.     return true;  
    8. }) (  
    9. Console.log (b);  //  print output   true  


(3)参数Arguments 

传递参数给IIFE 
JS Code
    1. (function (window) {
    2. //Can be called to window here
    3. }) (window);
    4. (function (window, document) {
    5. //This can be called to window and document
    6. }) (window, document);


undefined参数 
在ECMAScript 3中undefined是mutable的,这意味着可以给undefined赋值,而在ECMASCript 5的strict模式(‘use strict‘;)下是不可以的,解析式时会报语法错。 

所以为了保护IIFE的变量需要undefined参数: 
JS Code
    1. (function (window, document, undefined) {
    2. // ...  
    3. }) (window, document);


即使有人给undefined赋值也没有关系: 
JS Code
    1. undefined = true;
    2. (function (window, document, undefined) {
    3. //undefined points to a local undefined variable
    4. }) (window, document);


(4)代码压缩Minifying 
JS Code
    1. (function (window, document, undefined) {
    2. Console.log (window); //Window object
    3. }) (window, document);
代码压缩后,undefined的参数不再存在,但是由于 (window, document); 的调用没有传递第三个参数,所有c依然是一个本地undefined变量,所有参数中undefined的名字是无所谓什么的,只需要知道他指向的是一个undefined变量。 
JS Code
    1. (function (A, B, c) {
    2. Console.log (a); //Window object
    3. }) (window, document);


除undefined变量外,其他的所有希望只在函数内部有效的变量都可以通过参数传递进去,比如以下的jQuery对象。 
JS Code
      1. (function  ($, window ,  document, undefined)  {  
      2.   //  Use  $  to point to  jquery, for example:   
      3.   // $ ( Document). addclass (' test ');   
      4. }) (Jquery, window, document);  
      5.   
      6. (function  (a, b, c, d)  {   
      7.   //  code will be compressed to:   
      8.    // a (c). addclass (' test ');   
      9. }) (Jquery, window,  document);  
 

(function () {}) This is an expression

(function () {
var r, url;
var s = location.href;
url =/http:\/\/www.csdn.com.cn/i;
if (S.search (URL) >=0) {
var Head=document.getelementbyid ("Tophead");
Head.style.width= "990px";
}
})
An anonymous function was declared
Add a () to execute it

That is, you do not need a method name to directly create a call

function a(){alert()}  a()  //执行该方法 function { return alert()}()  //直接执行该方法

JS closure anonymous function JavaScript iife (Instant execution method) immediately-invoked function expression

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.