Skip to content

这个 Keyword

如何这个是 determined 通过 call site: 默认, implicit, explicit, and 新 binding.

你将学到

  • four binding 规则 for 这个
  • 默认 binding and strict-模式 undefined 情况
  • Implicit binding 通过 call-site 对象
  • Explicit binding 使用 call, apply, and bind
  • 新 binding and lexical 这个 in arrow 函数

概念讲解

What Is this?

这个 is a special keyword that refers to the object that "owns" the current execution. Unlike in some languages, 这个 in JavaScript is not fixed by where a function is defined — it is determined by how the function is called (the call site). Understanding the four binding rules is the key to predicting what 这个 will be.

1. Default Binding

A plain function call like foo() has default binding. In non-strict mode, 这个 is the global object (window in browsers, 全局 in Node). In strict mode, 这个 is undefined. This is the fallback when no other rule applies.

2. Implicit Binding

When a function is called as a method on an object (obj.foo()), this is that object. This is implicit binding. However, the binding is lost if you detach the method: const fn = obj.foo; fn(); now has default binding. This is the classic "lost this" bug when passing methods as callbacks.

3. Explicit Binding

You can force 这个 with call, apply, and bind. fn.call(obj, arg1, arg2) and fn.apply(obj, [arg1, arg2]) call the function immediately with 这个 set to obj. fn.bind(obj) returns a new function with 这个 permanently bound — useful for event handlers and callbacks that would otherwise lose their 这个.

4. new Binding

When a function is called with 新, a new object is created and 这个 is bound to it. This is how constructors work. The 新 keyword overrides all other binding rules.

Arrow Functions: Lexical this

Arrow functions do not follow these rules. They have no 这个 of their own and inherit it from the surrounding lexical scope. This makes them ideal for callbacks inside methods where you want 这个 to stay bound to the outer method's object. When you need a function's own 这个 (object methods, constructors), use a regular function.

示例代码

              // 1. Default binding — 'this' is undefined in strict mode
function showThis() {
  "use strict";
  console.log("default:", this);
}
showThis(); // undefined (strict mode)

// 2. Implicit binding — 'this' is the object before the dot
const counter = {
  count: 0,
  increment() {
    this.count++;
    return this.count;
  },
};
console.log("implicit:", counter.increment()); // 1

// Lost 'this' — detaching the method breaks implicit binding
const detached = counter.increment;
try {
  detached(); // TypeError in strict mode
} catch (e) {
  console.log("lost this:", e.message);
}

// 3. Explicit binding with call, apply, and bind
function greet(greeting, punct) {
  return `${greeting}, ${this.name}${punct}`;
}
const person = { name: "Ada" };
console.log(greet.call(person, "Hello", "!"));   // "Hello, Ada!"
console.log(greet.apply(person, ["Hi", "."]));    // "Hi, Ada."
const boundGreet = greet.bind(person, "Hey");
console.log(boundGreet("?")); // "Hey, Ada?"

// 4. Arrow function keeps lexical 'this' inside a method
const timer = {
  label: "Timer",
  start() {
    setInterval(() => {
      console.log(this.label, "tick"); // 'this' is timer, not the interval
    }, 2000);
  },
};
            

Demonstrates 所有 four binding 规则: 默认 (strict-模式 undefined), implicit (方法 call), lost-这个 bug, explicit binding 使用 call/apply/bind, and lexical 这个 in arrow 函数回调.

动手试一试

  • Math Evaluator

    方法那个 use 这个经常执行 calculations — 测试 logic.

  • Regex Tester

    Regex 方法如 测试 and exec depend on 这个 binding.

常见错误

错误写法

Losing 这个当 passing 方法 as 回调

正确做法

const fn = obj.method detaches the method and this becomes undefined or the global. Use fn = obj.method.bind(obj), an arrow wrapper () => obj.method(), or method shorthand in a class.

错误写法

Using arrow 函数 for 方法那个需要它的 own 这个

正确做法

Arrow 函数 inherit 这个从 surrounding 作用域 and cannot 是 rebound. For 对象方法 and 类方法那个 rely on 这个, use regular 函数语法 or 方法 shorthand.

相关速查表

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误