箭頭函數this
箭頭函數的this是繼承父執行上下文裏面的this
例子:
var x = 11;
var obb = {
x: 222,
y: {
x:333,
obc: function f() {
console.log(this)
var x = 111;
var obj = {
x: 22,
say: () => {
console.log(this.x);
}
}
obj.say()
}
}
}
obb.y.obc()
輸出結果是333,
原因箭頭函數沒有this,剪頭函數的this是繼承父執行上下文裏面的this ,這裏箭頭函數的執行上下文是函數f(),所以它就繼承了f()的this,
function this
function中的this是函數的調用對象
參考: https://blog.csdn.net/cjgeng88/article/details/79846670
※ 例子(一例頂千言)
var a = {
foo: () => {
console.log(this) ;
} ,
boo: function() {
console.log(this) ;
} ,
coo: function() {
return () => {
console.log(this);
}
}
} ;
var b = a.boo ;
a.foo() ;
a.boo() ;
b();
a.coo()() ;
參考:
-
function中的this - https://blog.csdn.net/cjgeng88/article/details/79846670
-
箭頭函數中的this - https://blog.csdn.net/weixin_42519137/article/details/88053339