问一个关于Javascript中bind函数的问题
 发布于 11 年前  作者 zhongao  3654 次预览  最后一次回复是 11 年前  来自 问答 

先上代码

var foo = {
  message: 'Foooooooo'
};

var bar = {
  message: 'Barrrrrrr'
};

function say() {
  console.log(this.message);
}

say();
say.bind(foo)();
say.bind(foo).bind(bar)();
say.bind(foo).apply(bar);

// 执行之后如下
// undefined
// Foooooooo
// Foooooooo
// Foooooooo

我的问题是:为什么bind(foo)之后,不管是再bind其它对象,还是用apply改变this对象,都无法影响调用结果? 我个人理解为应该按最后bind或apply的this来调用才对啊。

2 回复
tulayang
Function.prototype.bind = function (ctx) {
    var args = Array.prototype.slice.call(arguments, 1);
    return function () {
	    return this.apply(ctx, args.concat(arguments));
	};
};
alsotang

@tulayang 正解!

再 bind 已经无用,已被闭包锁死