调用指令:对apply/call/bind的理解

Function Invocation

1
2
3
4
5
6
7
8
    function hello(thing) 
    { 
        console.log(this + " says hello " + thing);
    }

    hello.call("Yehuda", "world") 

    //=> Yehuda says hello world  

简单的函数调用

1
2
3
4
5
6
7
8
9
10
11
12
    function hello(thing) {  
        console.log("Hello " + thing);
    }

    // this:
    hello("world")

    // es5模式下等价于:
    hello.call(window, "world");  

    // es5 严格模式下等价于
    hello.call(undefined,'world');

函数指令的理解(function invocation)

一个函数指令例如:fn(…args) 等价于 fn.call(window [ES5-strict: undefined], …args).

匿名函数(function() {})() 等价于 (function(){}).call(window [ES5-strict: undefined).

####方法调用

1
2
3
4
5
6
7
8
9
10
11
12
    var person = {  
        name: "Brendan Eich",
        hello: function(thing) {
            console.log(this + " says hello " + thing);
        }
    }

    // this:
    person.hello("world")

    // 等价于desugars to this:
    person.hello.call(person, "world");  

使用 Function.prototype.bind

有时为了方便对函数中的常量进行引用

1
2
3
4
5
6
7
8
9
10
11
12
13
    var person = {  
        name: "Brendan Eich",
        hello: function(thing) {
            console.log(this.name + " says hello " + thing);
        }
    }
    var boundHello = function(thing) { 
        return person.hello.call(person, thing); 
        //第二个person保证了对person对象name的调用
    }
    boundHello("world");  

    //Brendan Eich says hello world

参考文档