<script>
"use strict";
var a=function(){
console.log("a")
if(this){
this()
}
}
var b=function(){
console.log("b")
if(this){
this()
}
}
var c=function(){
console.log("c")
if(this){
this()
}
}
var d=a.bind(b)
var e=d.bind(c)
d()
e()
</script>
这段代码为什么执行结果是abab不能理解。。。
<script>
"use strict";
var a=function(){
console.log("a")
let t=eval("this")
if(this){
this()
t()
}
}
var b=function(){
console.log("b")
let t=eval("this")
if(this){
this()
t()
}
}
var c=function(){
console.log("c")
let t=eval("this")
if(this){
this()
t()
}
}
var x=function(){
console.log("a")
let t=eval("this")
if(b){
b()
t()
}
}
var d=a.bind(b)
var e=d.bind(c)
var f=x.bind(c)
e()
f()
</script>
就是这个“相当于”不理解啊,上面这个例子,是abbabc啊,说明第一次bind和并没有替换内部逻辑,纯粹就是第二次bind没有改变this的指向。
把 b bind 到 a 上时,d 就相当于
所以 d 再 bind 谁都没影响