Function

在开始我们的function 之前, 我们应该理解两个知识点:

函数表达式  Function Expression:

  var test = function() {}; 

函数申明  Function Declaration:

  function test() {};

Callback Function

注意: 我们是传入的值(function), arrayCalc 在运行的时候运行function. 而不是在一开始call function.

<script>
        var year = [1990, 1991, 1992, 1993, 1994];

        // callback function
        function arrayCalc(arr, fn) {
            var arrReult = [];
            for (let i = 0; i < arr.length; i++) {
                arrReult.push(fn(arr[i]));
            }
            return arrReult;
        }

        function calculateAge(el) {
            return 2018 - el;
        }

        // due to we are not to call the function, we only need pass in the value. 
        // So we only need calculateAge not calculateAge()
        // This is called callback function
        console.log(arrayCalc(year, calculateAge)); // [28, 27, 26, 25, 24]

    </script>

Functions Returning Functions

JS中是可以返回functions的.

<script>
        function interviewQuestion(job) {
            if (job === 'designer') {
                return function (name) {
                    console.log(name + ', can you pls explain what UX design is?');
                }
            } else {
                return function (name) {
                    console.log('hello ' + name + ', what can ' + job + ' do?');
                }
            }
        };

        // call function
        var designerQuestion = interviewQuestion('designer');
        console.log(designerQuestion);
        // designerQuestion will get a function and we need pass value in for this function.
        designerQuestion('John');

        // call function
        // because first part interviewQuestion('teacher') returns function
        // we can directly write second input value follow it.
        interviewQuestion('teacher')('Chris');
    </script>

Immediately Invoked Function Expressions (IIFE) 立即执行的函数表达式

IIFE: Immediately Invoked Function Expression,意为立即调用的函数表达式,也就是说,声明函数的同时立即调用这个函数。
对比一下,这是不采用IIFE时的函数声明和函数调用:

function foo(){
  var a = 10;
  console.log(a);
}
foo();

IIFE函数调用:

(function foo(){
  var a = 10;
  console.log(a);
})();

在函数的声明中,我们首先看到的是function关键字,而IIFE我们首先看到的是左边的(。也就是说,使用一对()将函数的声明括起来,使得JS编译器不再认为这是一个函数声明,而是一个IIFE,即需要立刻执行声明的函数。
两者达到的目的是相同的,都是声明了一个函数foo并且随后调用函数foo。

IIFE 的用处

如果只是为了立即执行一个函数,显然IIFE所带来的好处有限。实际上,IIFE的出现是为了弥补JS在scope方面的缺陷:JS只有全局作用域(global scope)、函数作用域(function scope),从ES6开始才有块级作用域(block scope)。对比现在流行的其他面向对象的语言可以看出,JS在访问控制这方面是多么的脆弱!那么如何实现作用域的隔离呢?在JS中,只有function,只有function,只有function才能实现作用域隔离,因此如果要将一段代码中的变量、函数等的定义隔离出来,只能将这段代码封装到一个函数中。

在我们通常的理解中,将代码封装到函数中的目的是为了复用。在JS中,当然声明函数的目的在大多数情况下也是为了复用,但是JS迫于作用域控制手段的贫乏,我们也经常看到只使用一次的函数:这通常的目的是为了隔离作用域了!既然只使用一次,那么立即执行好了!既然只使用一次,函数的名字也省掉了!这就是IIFE的由来。

IIFE的函数名和参数

IIFE一般是不需要function name, 如果必须要起一个, 可以使用IIFE. IIFE 也可以带多个参数.

var a = 2;
(function IIFE(global){
    var a = 3;
    console.log(a); // 3
    console.log(global.a); // 2
})(window);

console.log(a); // 2

IIFE 例子:

var myModule = (function module(){
  var someThing = "123";
  var otherThing = [1,2,3];

  function doSomeThing(){
    console.log(someThing);
  }

  function doOtherThing(){
    console.log(otherThing);
  }

  return {
    doSomeThing:doSomeThing,
    doOtherThing:doOtherThing
  }
})();

myModule.doSomeThing();
myModule.doOtherThing();
原文地址:https://www.cnblogs.com/TheMiao/p/9742464.html