js 中的 this 指向十分重要,了解 js 中 this 指向是每一个学习js的人必学的知识点,今天没事,正好总结了 js 中 this 的常见用法,喜欢的可以看看:
1. 全局作用域或者普通函数中 this 指向全局对象 window。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14//直接打印 console.log(this) //window //function声明函数 function bar () {console.log(this)} bar() //window //function声明函数赋给变量 var bar = function () {console.log(this)} bar() //window //自执行函数 (function () {console.log(this)})(); //window
2. 方法调用中谁调用 this 指向谁
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30//对象方法调用 var person = { run: function () {console.log(this)} } person.run() // person //事件绑定 var btn = document.querySelector("button") btn.onclick = function () { console.log(this) // btn } //事件监听 var btn = document.querySelector("button") btn.addEventListener('click', function () { console.log(this) //btn }) //jquery的ajax $.ajax({ self: this, type: "get", url: url, async: true, success: function (res) { console.log(this) // this指向传入$.ajxa()中的对象 console.log(self) // window } }); //这里说明以下,将代码简写为$.ajax(obj) ,this指向obj,在obj中this指向window,因为在在success方法中,独享obj调用自己,所以this指向obj
3. 在构造函数或者构造函数原型对象中 this 指向构造函数的实例
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16//不使用new指向window function Person(name) { console.log(this) // window this.name = name; } Person('inwe') //使用new function Person(name) { this.name = name console.log(this) //people self = this } var people = new Person('iwen') console.log(self === people) //true //这里new改变了this指向,将this由window指向Person的实例对象people
4. 箭头函数中指向外层作用域的 this
复制代码
1
2
3
4
5
6
7
8
9
10
11
12var obj = { foo() { console.log(this); }, bar: () => { console.log(this); } } obj.foo() // {foo: ƒ, bar: ƒ} obj.bar() // window
最后
以上就是迷路乐曲最近收集整理的关于彻底弄懂js中的this指向的全部内容,更多相关彻底弄懂js中内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复