JavaScript 有一套完全不同于其它语言的对 this 的处理机制. 在五种不同的情况下 ,this 指向的各不相同.
1. 全局范围
this;
当在全部范围内使用 this, 它将会指向全局对象.
- // 直接打印
- 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. 函数调用
foo();
这里 this 也会指向全局对象.
- // 直接打印
- 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
3. 方法调用
test.foo();
这个例子中, this 指向 test 对象.
- // 对象方法调用
- 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
4. 调用构造函数
new foo();
如果函数倾向于和 new 关键词一块使用, 则我们称这个函数是 构造函数. 在函数内部, this 指向新创建的对象.
- // 不使用 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
当 this 碰到 return 时
- function fn()
- {
- this.user = 'sssss';
- return {};
- }
- var a = new fn;
- console.log(a.user); //undefined
- function fn()
- {
- this.user = 'sssss';
- return function(){};
- }
- var a = new fn;
- console.log(a.user); //undefined
- function fn()
- {
- this.user = 'sssss';
- return 1;
- }
- var a = new fn;
- console.log(a.user); //sssss
- function fn()
- {
- this.user = 'sssss';
- return undefined;
- }
- var a = new fn;
- console.log(a.user); //sssss
如果返回值是一个对象, 那么 this 指向的就是那个返回的对象, 如果返回值不是一个对象那么 this 还是指向函数的实例.
- function fn()
- {
- this.user = 'sssss';
- return undefined;
- }
- var a = new fn;
- console.log(a); //fn {user: "sssss"}
- // 还有一点就是虽然 null 也是对象, 但是在这里 this 还是指向那个函数的实例, 因为 null 比较特殊.
- function fn()
- {
- this.user = 'sssss';
- return null;
- }
- var a = new fn;
- console.log(a.user); //sssss
5. 显示的设置 this
- function foo(a, b, c) {}
- var bar = {};
- foo.apply(bar, [1, 2, 3]); // 数组将会被扩展, 如下所示
- foo.call(bar, 1, 2, 3); // 传递到 foo 的参数是: a = 1, b = 2, c = 3
当使用 Function.prototype 上的 call 或者 apply 方法时, 函数内的 this 将会被 显式设置为函数调用的第一个参数.
因此函数调用的规则在上例中已经不适用了, 在 foo 函数内 this 被设置成了 bar.
常见误解
尽管大部分的情况都说的过去, 不过第一个规则 (译者注: 这里指的应该是第二个规则, 也就是直接调用函数时, this 指向全局对象) 被认为是 JavaScript 语言另一个错误设计的地方, 因为它从来就没有实际的用途.
- Foo.method = function() {
- function test() {
- // this 将会被设置为全局对象 (译者注: 浏览器环境中也就是 window 对象)
- }
- test();
- }
一个常见的误解是 test 中的 this 将会指向 Foo 对象, 实际上不是这样子的.
为了在 test 中获取对 Foo 对象的引用, 我们需要在 method 函数内部创建一个局部变量指向 Foo 对象.
- Foo.method = function() {
- var that = this;
- function test() {
- // 使用 that 来指向 Foo 对象
- }
- test();
- }
that 只是我们随意起的名字, 不过这个名字被广泛的用来指向外部的 this 对象. 在 闭包 一节, 我们可以看到 that 可以作为参数传递.
方法的赋值表达式
另一个看起来奇怪的地方是函数别名, 也就是将一个方法赋值给一个变量.
- var test = someObject.methodTest;
- test();
上例中, test 就像一个普通的函数被调用; 因此, 函数内的 this 将不再被指向到 someObject 对象.
虽然 this 的晚绑定特性似乎并不友好, 但这确实是基于原型继承赖以生存的土壤.
- function Foo() {}
- Foo.prototype.method = function() {};
- function Bar() {}
- Bar.prototype = Foo.prototype;
- new Bar().method();
当 method 被调用时, this 将会指向 Bar 的实例对象.
来源: http://www.qdfuns.com/article/18271/851c34f78d007a4d75c7133517eda9d7.html