call, apply, bind的区别

call/apply/bind改变函数运行时候的上下文

  • bind 的实现
    1
    2
    3
    4
    5
    6
    7
    Function.prototype.bind = function(obj) {
    var method = this;
    var args = [].slice.call(arguments, 1);
    return function() {
    method.apply(obj, args.concat([].slice.call(arguments)));
    }
    }
  • bind/apply/call的区别
    bind: 只是改变了this指向,没有立即调用
    apply: 传入的参数需要是数组,立即调用
    call: 传入正常的参数,立即调用
  • 使用场景
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    var people = {
    sayName: function(age){
    debugger
    console.log(this.name, age);
    }
    };
    var person = {
    name: 'marong'
    }
    people.sayName.call(person, 22);
    people.sayName.bind(person)(22);
    people.sayName.apply(person, [22]);