this

this

函数的this

单个函数的this是什么

window

嵌套函数的this

1
2
3
4
5
6
7
8
9
function parent() {
let name = "parent";
function son() {
let name = "son";
console.log(this);
}
son();
}
parent();

输出也是window

对象

单层函数

1
2
3
4
5
6
7
let obj = {
name: "obj",
sayHi: function () {
console.log(this);
},
}
obj.sayHi();

obj 这个对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<script>
let parent = {
name: "parent",
son() {
let name = "son";
let arrow = () => {
console.log("arrow", this);
};
function th() {
console.log("th", this);
}
return {
name: "re",
arrow,
th,
}
}
}
parent.son().arrow();
parent.son().th();
</script>

1
2
arrow {name: 'parent', son: ƒ}
th {name: 're', arrow: ƒ, th: ƒ}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<script>
grandPa = {
name: "gradnPa",
parent() {
// let name = "parent";
return function son() {
let name = "son";
let arrow = () => {
console.log("arrow", this);
};
function th() {
console.log("th", this);
}
return {
name: "re",
arrow,
th,
}
}
}
}
grandPa.parent()().arrow();
grandPa.parent()().th();
</script>

arrow Window {window: Window, self: Window, document: document, name: ‘’, location: Location, …}
th {name: ‘re’, arrow: ƒ, th: ƒ}

打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2022 Jie Shen
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信