前言

最近遇到个多次bind绑定的问题,看下面代码你预测下会输出什么

var a=function(){
console.log("a", this)
if(typeof this === "function"){
this()
}
}

var b=function(){
console.log("b")
if(typeof this === "function"){
this()
}
}

var c=function(){
console.log("c")
if(typeof this === "function"){
this()
}
}

var d=a.bind(b)
var e=d.bind(c)
d()
e()
点击查看输出
// abab

我们可以先看看 abind的时候发生了什么

d = function() {
console.log("a")
if(typeof b === "function"){
b()
}
}

abind的时候把函数内的this都替换成了 函数b,所以当 d再去bind时候已经无意义了