未经过||判定,this指向的是字面量
console.log(({
Y() {
return 1
},
X() {
console.log(this)
return this.Y()
}
}['X'])())
经过||判定后,this指向的是window
console.log(({
Y() {
return 1
},
X() {
console.log(this)
return this.Y()
}
}['X'] || {})())
这是为什么,按理说两种情况字面量都是通过输入的'x'返回相应函数,为什么经过||判断后函数的作用域会不一样?
http://zonxin.github.io/post/2015/11/jav...
看这个吧,懒得在写一遍了
第二段代码使用了||
,调用符号()
左面由引用类型变成了表达式,返回了函数对象,因为不是引用类型,this的值是global(window)