Javascript 编程基础(4)函数 | 4.2、this 绑定机制

一、this 绑定机制

1、原理分析

this 是 JavaScript 中最容易混淆的概念之一,它的绑定机制决定了函数执行时的上下文。理解 this 的工作原理对于编写可靠的 JavaScript 代码至关重要。

1.1、动态绑定特性

this 的值不是在函数定义时确定的,而是在函数被调用时动态绑定的。这与大多数编程语言中的 thisself 概念有显著区别。

1.2、绑定规则优先级

JavaScript 按照以下优先级顺序确定 this 的绑定:

  1. new 绑定(构造函数调用)
  2. 显式绑定(call/apply/bind)
  3. 隐式绑定(方法调用)
  4. 默认绑定(普通函数调用)

1.3、执行上下文

this 实际上是执行上下文的一个属性,每次函数调用都会创建一个新的执行上下文,其中的 this 值由调用方式决定。

2、绑定类型详解

2.1、默认绑定(独立函数调用)

  • 非严格模式下指向全局对象(浏览器中为 window,Node.js 中为 global
  • 严格模式下为 undefined
function foo() {
  console.log(this); 
}

foo(); // 浏览器中输出 window(非严格模式)

2.2、 隐式绑定(方法调用)

  • 函数作为对象方法调用时,this 指向调用该方法的对象
  • 存在隐式丢失问题
const obj = {
  name: 'Alice',
  greet: function() {
    console.log(`Hello, ${this.name}`);
  }
};

obj.greet(); // "Hello, Alice"(this 指向 obj)

// 隐式丢失示例
const greetFn = obj.greet;
greetFn(); // "Hello, undefined"(this 指向全局)

2.3、显式绑定(call/apply/bind)

  • 直接指定 this 的值
  • bind 创建永久绑定,call/apply 立即执行
function introduce(lang) {
  console.log(`${this.name} codes in ${lang}`);
}

const dev = { name: 'Bob' };

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

// apply(参数为数组)
introduce.apply(dev, ['Python']); 

// bind(创建新函数)
const boundFn = introduce.bind(dev, 'Java');
boundFn();

2.4、new 绑定(构造函数)

  • this 指向新创建的对象实例
  • 自动执行以下步骤:
    1. 创建新对象
    2. this 绑定到新对象
    3. 执行构造函数代码
    4. 返回新对象(除非显式返回其他对象)
function Person(name) {
  this.name = name;
}

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

2.5、箭头函数(词法绑定)

  • 继承定义时的外层 this
  • 无法通过 call/apply/bind 修改
  • 没有自己的 thisargumentssupernew.target
const outer = {
  name: 'Dave',
  inner: () => {
    console.log(this.name); // 继承定义时的 this
  },
  properInner: function() {
    setTimeout(() => {
      console.log(this.name); // 继承 properInner 的 this
    }, 100);
  }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值