1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > JavaScript对象this指向(普通键this指向 非指向函数的键)

JavaScript对象this指向(普通键this指向 非指向函数的键)

时间:2023-08-16 21:41:23

相关推荐

JavaScript对象this指向(普通键this指向 非指向函数的键)

1、结论

JavaScript对象普通键(非指向函数的键)this指向是window。

2、示例

<!DOCTYPE html><html lang="zh"><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>JavaScript对象this指向(普通键this指向 非指向函数的键)</title></head><body><script type="text/javascript">var a=1;var b = {a: 2,c: this.a + 10,d: eval(console.log(this)),getA: function() {console.log(this.a);}};//setTimeout的this指向window,相当于window.setTimeout setTimeout(b.getA, 10); //1//this指向window console.log(b.c); //11 //this指向b对象 b.getA(); //2</script></body></html>

输出:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。