1箭頭函數中的this
- (1)
箭頭函數
中沒有this
: 這意味着 call() apply() bind() 無法修改箭頭函數中的this
- (2)箭頭函數中的this指向 :
訪問上一個作用域的this
- 說人話:函數在哪個作用域聲明,this就是誰 (本質是通過作用域鏈訪問上一個作用域中的this)
- (3)箭頭函數與function函數this區別
- function函數 : 誰
調用
我,我就指向誰,與聲明無關
- 箭頭函數 : 誰
聲明
我,我就指向誰 ,與調用無關
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
let fn = () => {
console.log(this);
};
fn();
fn.call({ a: 11 });
let obj = {
sayHi: () => {
console.log('學習使我快樂');
console.log(this);
}
};
obj.sayHi();
var person = {
play: function () {
console.log('play中的this');
console.log(this);
let fn2 = () => {
console.log('fn2在一級鏈中被聲明,我的this就是一級鏈中的this');
console.log(this);
};
fn2();
}
};
person.play();
</script>
</body>
</html>