js-bind 的多次绑定
前言
最近遇到个多次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
我们可以先看看 a在bind的时候发生了什么
d = function() {
    console.log("a")
    if(typeof b === "function"){
        b()
    }
}
a在bind的时候把函数内的this都替换成了 函数b,所以当d再去bind时候已经无意义了
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 kshao-blog-前端知识记录!
 评论







