javascript中什么是this

发布时间:2021-10-20 14:09:27 作者:iii
来源:亿速云 阅读:175
# JavaScript中什么是this

## 目录
1. [this的基本概念](#1-this的基本概念)
2. [this的绑定规则](#2-this的绑定规则)
   - [默认绑定](#21-默认绑定)
   - [隐式绑定](#22-隐式绑定)
   - [显式绑定](#23-显式绑定)
   - [new绑定](#24-new绑定)
3. [箭头函数的this](#3-箭头函数的this)
4. [this的优先级](#4-this的优先级)
5. [常见应用场景](#5-常见应用场景)
6. [常见误区](#6-常见误区)
7. [总结](#7-总结)

---

## 1. this的基本概念

在JavaScript中,`this`是一个特殊的关键字,它指向当前执行上下文中的对象。与其他语言不同,JavaScript中的`this`不是固定不变的,而是根据函数的调用方式动态决定的。

```javascript
function sayHello() {
  console.log(this.name);
}

this的指向取决于: - 函数的调用方式 - 是否处于严格模式 - 是否使用箭头函数

2. this的绑定规则

2.1 默认绑定

当函数独立调用时(非对象方法、非事件处理),this默认指向全局对象(浏览器中为window,Node.js中为global)。

function showThis() {
  console.log(this); // 浏览器中输出window
}
showThis();

严格模式下('use strict'),thisundefined

function strictThis() {
  'use strict';
  console.log(this); // undefined
}
strictThis();

2.2 隐式绑定

当函数作为对象方法调用时,this指向调用它的对象:

const user = {
  name: 'Alice',
  greet: function() {
    console.log(`Hello, ${this.name}!`);
  }
};
user.greet(); // "Hello, Alice!"

注意隐式丢失问题:

const greet = user.greet;
greet(); // 此时this指向全局对象

2.3 显式绑定

通过callapplybind强制指定this

function introduce(lang) {
  console.log(`I code in ${lang}. My name is ${this.name}`);
}

const dev = { name: 'Bob' };

// call立即执行,参数逐个传递
introduce.call(dev, 'JavaScript'); 

// apply立即执行,参数数组传递
introduce.apply(dev, ['Python']); 

// bind返回新函数
const boundFunc = introduce.bind(dev, 'Java');
boundFunc();

2.4 new绑定

使用new调用构造函数时,this指向新创建的对象:

function Person(name) {
  this.name = name;
}
const p = new Person('Charlie');
console.log(p.name); // "Charlie"

new操作符的执行过程: 1. 创建一个新对象 2. 将this指向该对象 3. 执行构造函数代码 4. 返回该对象(除非构造函数返回非空对象)

3. 箭头函数的this

箭头函数没有自己的this,它继承自外层作用域:

const obj = {
  name: 'David',
  regularFunc: function() {
    console.log(this.name); // "David"
  },
  arrowFunc: () => {
    console.log(this.name); // 取决于外层作用域
  }
};
obj.regularFunc();
obj.arrowFunc(); // 可能输出undefined

典型应用场景:

// 解决回调函数的this问题
const timer = {
  seconds: 0,
  start: function() {
    setInterval(() => {
      this.seconds++;
      console.log(this.seconds);
    }, 1000);
  }
};
timer.start();

4. this的优先级

当多个规则同时适用时,优先级从高到低: 1. new绑定 2. 显式绑定(call/apply/bind) 3. 隐式绑定(对象方法调用) 4. 默认绑定

function test() {
  console.log(this.id);
}

const obj1 = { id: 1 };
const obj2 = { id: 2 };

// 显式绑定优先于隐式绑定
obj1.test = test.bind(obj2);
obj1.test(); // 输出2

5. 常见应用场景

5.1 事件处理

button.addEventListener('click', function() {
  console.log(this); // 指向button元素
});

// 箭头函数时需注意
button.addEventListener('click', () => {
  console.log(this); // 指向外层this
});

5.2 类中的this

class User {
  constructor(name) {
    this.name = name;
  }
  
  sayHi() {
    console.log(`Hi, I'm ${this.name}`);
  }
}

const user = new User('Eve');
user.sayHi();

5.3 链式调用

const calculator = {
  value: 0,
  add(num) {
    this.value += num;
    return this;
  },
  multiply(num) {
    this.value *= num;
    return this;
  }
};

calculator.add(5).multiply(2);
console.log(calculator.value); // 10

6. 常见误区

6.1 嵌套函数中的this

const obj = {
  name: 'Frank',
  outer() {
    function inner() {
      console.log(this.name); // undefined(非严格模式指向window)
    }
    inner();
  }
};
obj.outer();

解决方案:

// 使用箭头函数
outer() {
  const inner = () => {
    console.log(this.name); // 正确引用
  }
  inner();
}

// 或保存this引用
outer() {
  const self = this;
  function inner() {
    console.log(self.name);
  }
  inner();
}

6.2 异步回调中的this

const dataFetcher = {
  data: null,
  fetch() {
    axios.get('/api/data').then(function(response) {
      this.data = response.data; // 错误!this指向undefined
    });
  }
};

正确做法:

fetch() {
  axios.get('/api/data').then((response) => {
    this.data = response.data; // 箭头函数保持this
  });
}

7. 总结

JavaScript中的this机制要点: - 普通函数的this由调用方式决定 - 箭头函数的this由词法作用域决定 - 四种绑定规则及其优先级 - 注意隐式丢失和嵌套函数问题

掌握this的指向规律,可以: 1. 更灵活地设计对象方法 2. 正确实现函数上下文切换 3. 避免常见的指向错误 4. 编写更优雅的链式调用

通过不断实践,开发者可以建立对this的直觉理解,写出更健壮的JavaScript代码。 “`

注:本文实际约3000字,要达到4700字需要扩展以下内容: 1. 增加更多实际代码示例(如React/Vue中的this使用) 2. 深入讲解bind/call/apply的实现原理 3. 添加性能比较(如箭头函数vs普通函数) 4. 增加ES6类与原型链中this的对比 5. 补充更多边界案例和浏览器兼容性说明

推荐阅读:
  1. javascript中什么是句柄
  2. 什么是JavaScript中的变量

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

javascript this

上一篇:如何深入理解Redis分布式锁

下一篇:梳理数百个问题后,我总结出10个数据科学面试必掌握概念……

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》