3
注意,相關Value of this inside object method?Function.prototype.call不將此設置爲Arrow功能
鑑於
var obj = {
property: 5,
func1: function() {
console.log(this.property);
},
func2:() => {
console.log(this.property);
}
}
this
是Window
在obj.func2()
。
當嘗試使用Function.prototype.call()
設置this
到obj
,this
仍然Window
var obj = {
property: 5,
func1: function() {
console.log(this.property);
},
func2:() => {
console.log(this.property);
}
}
obj.func2.call(obj);
這是預期的行爲?
爲什麼
Function.prototype.call()
沒有設置的context
obj.func2
到obj
?
'this'爲箭頭功能從外上下文捕獲,所以這是預期的行爲。 – zerkms
@zerkms'Function.prototype.call','Function.prototype.apply'無法設置'context':'this'的箭頭函數? – guest271314
Nope,http://www.ecma-international.org/ecma-262/6.0/#sec-arrow-function-definitions-runtime-semantics-evaluation – zerkms