javaScript——this 的四种绑定:为什么箭头函数改不了它

1 阅读8分钟

四种绑定这个说法本身没问题,问题在于它被当成四张要背的表。这篇给一条更底层的判定:this 是「调用时传进来的隐藏参数」,判定只看调用表达式的形状 + 一条优先级链。箭头函数之所以改不了,是因为它压根不接收这个参数 —— 所以 call/apply/bind 是静默无效,不是报错。

统计日期:2026-11-26 | 基准环境:Node 22.22.2(Windows 11)/ Chrome 152.0.7977.75(headless) 观测手段:用 fn.call/apply/bind、new、赋值、解构、数组取出、逗号表达式等 11 种形态调用同一函数,比对 this 的引用身份;宿主差异用 CDP 在真实页面里取值 本文性质:全部结论实跑得出;跨环境差异给出两端的实测值

引子:把方法取出来,它就忘了自己是谁

const obj = {
  name: 'obj',
  show() { return this && this.name; },
};

console.log(`obj.show()  = ${obj.show()}`);

const g = obj.show;
console.log(`const g = obj.show; g()  = ${g()}`);

输出:

obj.show()  = obj
const g = obj.show; g()  = undefined

同一个函数,第一次拿到 'obj',第二次拿到 undefined。

函数一个字都没改,改变的是调用表达式的形状:obj.show() 里 show 前面有个 obj.,g() 里没有。

这就是理解 this 的入口:this 不是函数的属性,是调用时由「怎么写这个调用」决定的一个隐藏参数。


一、this 是调用时传入的隐藏参数

如果 this 是函数的属性,那么同一个函数无论怎么调用,this 都该一样。实测不是:

function who() { return this; }

const tag = (v) => {
  if (v === undefined) return 'undefined';
  if (v === globalThis) return 'globalThis';
  if (v && v.__tag) return v.__tag;
  if (v && v.constructor) return 'new(' + v.constructor.name + ')';
  return String(v);
};

const o = { __tag: 'obj', who };
const deep = { __tag: 'deep', mid: { __tag: 'mid', who } };

const cases = [
  ['who()', () => who()],
  ['obj.who()', () => o.who()],
  ['deep.mid.who()', () => deep.mid.who()],
  ['const f = o.who; f()', () => { const f = o.who; return f(); }],
  ['const { who: g } = o; g()', () => { const { who: g } = o; return g(); }],
  ['who.call(o)', () => who.call(o)],
  ['who.apply(o)', () => who.apply(o)],
  ['who.bind(o)()', () => who.bind(o)()],
  ['new who()', () => new who()],
  ['[o.who][0]()', () => [o.who][0]()],
  ['(0, o.who)()', () => (0, o.who)()],
];

console.log('调用形态 | this');
for (const [name, fn] of cases) {
  let r;
  try { r = tag(fn()); } catch (e) { r = '抛 ' + e.constructor.name; }
  console.log(`${name} | ${r}`);
}

输出:

调用形态 | this
who() | globalThis
obj.who() | obj
deep.mid.who() | mid
const f = o.who; f() | globalThis
const { who: g } = o; g() | globalThis
who.call(o) | obj
who.apply(o) | obj
who.bind(o)() | obj
new who() | new(who)
[o.who][0]() | new(Array)
(0, o.who)() | globalThis

同一个 who,十一种调用,六种不同的 this。

最后三行值得单独看,它们跟直觉相反:

  • new who() → this 是一个新对象(构造器名 who),不是 o,也不是全局。
  • [o.who][0]() → this 是那个数组。很多人以为这是「从数组里取出方法再调用,所以丢 this」,实际不是 —— 调用表达式是 <数组>[0](),[0] 前面的基对象是数组,所以 this 是数组本身。
  • (0, o.who)() → this 是 globalThis。逗号运算符返回的是值而不是引用,所以这里没有任何基对象可绑。

判据:this 由「调用点的形状」决定,与函数在哪里定义、定义了几次、叫什么名字全部无关。 唯一例外是箭头函数(第五章)。


二、优先级链:new > bind > call/apply > 隐式 > 默认

十一种形态不是并列的,它们之间有严格的优先级。三条容易出错的关系:

2.1 new 能覆盖 bind 绑定的 this

这条最反直觉:bind 常被说成「永久锁定 this」,但 new 能把它掀翻。

function P() { return this; }
const o1 = { __tag: 'o1' };
const tag = (v) => (v === undefined ? 'undefined' : v === globalThis ? 'globalThis'
  : (v && v.__tag) || (v && v.constructor && 'new(' + v.constructor.name + ')') || String(v));

const Bound = P.bind(o1);
console.log(`Bound() 的 this = ${tag(Bound())}`);

const inst = new Bound();
console.log(`new Bound() 的 this = ${tag(inst)}`);
console.log(`inst 是 P 的实例:${inst instanceof P}`);

输出:

Bound() 的 this = o1
new Bound() 的 this = new(P)
inst 是 P 的实例:true

Bound() 确实用 o1,但 new Bound() 用的是新建的实例,o1 被完全忽略。

判据:bind 只锁定「普通调用」的 this;new 的优先级更高,会覆盖它。 所以「bind 之后 this 就不可能变」这句话是错的 —— 它对 call/apply 成立,对 new 不成立。

2.2 bind 只认第一次,后面的静默失效

function P() { return this; }
const o1 = { __tag: 'o1' };
const tag = (v) => (v && v.__tag) || String(v);

const B2 = P.bind(o1).bind({ __tag: 'o2' });
console.log(`bind(o1).bind(o2) 的 this = ${tag(B2())}`);

输出:

bind(o1).bind(o2) 的 this = o1

第二次 bind 不报错,也不生效。bind 返回的是一个已经带 [[BoundThis]] 的函数,再 bind 它只是又包了一层,但内层的 [[BoundThis]] 优先。

判据:bind 是不可逆的,重复 bind 静默无效。 想换 this 只能重新 bind 原函数。

2.3 显式绑定覆盖隐式绑定

function P() { return this; }
const o1 = { __tag: 'o1' };
const tag = (v) => (v && v.__tag) || String(v);

const o3 = { __tag: 'o3', m: P };
console.log(`o3.m() 的 this = ${tag(o3.m())}`);
console.log(`o3.m.call(o1) 的 this = ${tag(o3.m.call(o1))}`);

输出:

o3.m() 的 this = o3
o3.m.call(o1) 的 this = o1

o3.m() 的隐式绑定被 call(o1) 直接盖掉。

完整优先级链:

new 绑定  >  显式绑定(bind / call / apply)  >  隐式绑定(a.b.f())  >  默认绑定

判据:判断 this 不要「找最像的那个」,要按这条链从上往下问,第一个成立的就是答案。 顺序错了,new 和 bind 打架时就会判错。


三、默认绑定为什么是 globalThis:严格模式只改这一种

默认绑定(直接调用 f())的 this 取决于函数自己是不是严格模式,不是调用方的:

function sloppy() { return this; }
function strict() { 'use strict'; return this; }
const tag2 = (v) => (v === undefined ? 'undefined' : v === globalThis ? 'globalThis'
  : (v && v.__tag) || String(v));

console.log(`非严格直接调用:${tag2(sloppy())}`);
console.log(`严格直接调用:${tag2(strict())}`);

const o4 = { __tag: 'o4', s: strict };
console.log(`严格函数作为方法:${tag2(o4.s())}`);

输出:

非严格直接调用:globalThis
严格直接调用:undefined
严格函数作为方法:o4

三行信息:

  1. 非严格直接调用 → globalThis(这是一种「兜底」,不是「绑定」)。
  2. 严格直接调用 → undefined。
  3. 严格函数作为方法调用 → 仍然绑定 o4。

判据:严格模式只影响「默认绑定」这一种,把 globalThis 换成 undefined。 隐式、显式、new 三种完全不受影响 —— 这就是为什么「严格模式下 this 总是 undefined」是错的。

顺带一条:class 体内的代码默认严格。所以类方法解构后 this 是 undefined,不是 globalThis(第六章实测)。


四、隐式绑定只看「最后一个点前面的对象」

a.b.c.f() 的 this 是 a.b.c,不是 a,也不是 a.b:

const deep = { __tag: 'deep', mid: { __tag: 'mid', f: function () { return this; } } };
console.log(`deep.mid.f() 的 this = ${deep.mid.f().__tag}`);

输出:

deep.mid.f() 的 this = mid

规则很机械:取调用表达式里「函数名左边那一个」作为 this。 三个容易翻车的变体:

const o = { __tag: 'obj', f: function () { return this; } };
const show = (v) => (v === globalThis ? 'globalThis' : (v && v.__tag) || ('new(' + (v && v.constructor && v.constructor.name) + ')'));

console.log(`[o.f][0]()      → ${show([o.f][0]())}`);
console.log(`(0, o.f)()      → ${show((0, o.f)())}`);
console.log(`(o.f)()         → ${show((o.f)())}`);

输出:

[o.f][0]()      → new(Array)
(0, o.f)()      → globalThis
(o.f)()         → obj

三个变体,三种结果:

写法this为什么
[o.f][0]()数组本身基对象是 [...] 这个数组,不是 o
(0, o.f)()globalThis逗号表达式返回值,没有基对象
(o.f)()obj括号不改变引用,基对象仍是 o

判据:括号无害,逗号致命,数组取出会换人。 判断时只问一句:调用括号左边紧挨着的是不是一个「引用」(a.b 或 a[i])?是,那个 a 就是 this;不是(值、字面量结果),就走默认绑定。

[o.f][0]() 这条几乎没人写对 —— 多数文章说它「丢了 this」,实际它是换成了数组。


五、箭头函数:它没有 this 这个参数

到这里可以回答标题的问题了。四种绑定描述的是「怎么给一个函数传 this」,而箭头函数根本不接收这个参数。

5.1 call / apply / bind 全部静默无效

function outer() {
  const arrow = () => this;
  return {
    direct: arrow(),
    called: arrow.call({ __tag: 'callArg' }),
    applied: arrow.apply({ __tag: 'applyArg' }),
    bound: arrow.bind({ __tag: 'bindArg' })(),
  };
}
const r = outer.call({ __tag: 'outerThis' });
console.log(`直接调用:${r.direct.__tag}`);
console.log(`call:${r.called.__tag}`);
console.log(`apply:${r.applied.__tag}`);
console.log(`bind:${r.bound.__tag}`);

输出:

直接调用:outerThis
call:outerThis
apply:outerThis
bind:outerThis

四种方式全部拿到 outerThis —— 也就是 outer 被调用时的 this。

注意没有报错。arrow.call(x) 不抛异常,它只是把 x 忽略掉了。

判据:箭头函数的 this 在「创建时」就固定为外层函数的 this,之后 call/apply/bind 全部静默失效。 「静默」是关键 —— 你以为改成功了,实际什么都没变,这类 bug 只能靠读代码发现。

5.2 它也不能 new

const arrow = () => 1;
try { new arrow(); } catch (e) { console.log(`new 箭头函数 → ${e.constructor.name}: ${e.message}`); }
console.log(`箭头函数有 prototype 吗:${'prototype' in arrow}`);
console.log(`普通函数有 prototype 吗:${'prototype' in function () {}}`);

输出:

new 箭头函数 → TypeError: arrow is not a constructor
箭头函数有 prototype 吗:false
普通函数有 prototype 吗:true

this 与「能被 new」是同一件事的两面:new 的本质是「创建一个新对象,把它作为 this 传进去」。箭头函数不接收 this,所以它也没有 [[Construct]],new 直接抛 TypeError。

判据:this、arguments、[[Construct]]、prototype 是一组绑在一起的能力 —— 箭头函数把这一组全丢了。 这也是它没有 prototype 属性的原因,而不是「语法节省」。

5.3 对象字面量里的箭头函数拿不到那个对象

这是最常见的误用:以为箭头函数写在对象里就会绑定对象。

const obj = {
  name: 'lit',
  arrowThis: () => this,
  normalThis() { return this; },
};
console.log(`对象里的箭头 this 是对象吗:${obj.arrowThis() === obj}`);
console.log(`对象里的普通方法 this 是对象吗:${obj.normalThis() === obj}`);

输出:

对象里的箭头 this 是对象吗:false
对象里的普通方法 this 是对象吗:true

对象字面量不创建作用域,所以箭头函数的「外层」是模块/脚本那一层,与 obj 无关。

判据:对象字面量的 {} 不是作用域,箭头函数写在里面也拿不到对象。 想拿对象只能写方法简写或普通函数。


六、丢 this 的三种形态,与 class 的陷阱

实际项目里 this 出问题,几乎都是「方法被取出来单独调用」:

class Counter {
  constructor() { this.n = 0; }
  inc() { this.n++; return this.n; }
}
const c = new Counter();
console.log(`方法调用正常:${c.inc()}`);

const detach = c.inc;
try { detach(); } catch (e) { console.log(`解构后调用 → ${e.constructor.name}: ${e.message}`); }

输出:

方法调用正常:1
解构后调用 → TypeError: Cannot read properties of undefined (reading 'n')

注意错误是 TypeError(不能读 undefined 的属性),不是「this 不对」。因为 class 体内默认严格,解构后的 this 是 undefined,于是 this.n 抛错。

三种形态,机制相同:

形态写法结果
赋值const f = obj.m; f()默认绑定(严格 → undefined)
解构const { m } = obj; m()同上
作为回调setTimeout(obj.m, 0) / arr.map(obj.m)同上

判据:方法一旦离开「点」就丢 this;class 方法因为默认严格,丢得更彻底(undefined 而不是 globalThis)。 所以「解构后调用报 Cannot read properties of undefined」这个错误,第一反应该查 this,而不是查参数。

三种固定手段,各有代价:

class Fixed {
  constructor() { this.n = 0; }
  arrow = () => { this.n++; return this.n; };   // 类字段:每个实例一份
  bound() { return this; }
}

const f = new Fixed();
const detachArrow = f.arrow;
console.log(`类字段箭头可解构:${detachArrow()}`);

const detachBound = f.bound.bind(f);
console.log(`bind 后可解构:${detachBound() === f}`);

输出:

类字段箭头可解构:1
bind 后可解构:true

判据:类字段箭头函数「每个实例各有一份」,适合当回调;bind 返回新函数,适合一次性固定。 前者省心但多占内存(实例数 × 函数对象),后者省内存但要在构造时记得 bind。


七、宿主环境决定「默认值」:三张不同的脸

同样的 setTimeout 回调,同一个 this,三个环境三个答案:

setTimeout(function () {
  console.log(`Node 里 setTimeout 回调 this 的构造器:${this && this.constructor && this.constructor.name}`);
}, 10);

输出:

Node 里 setTimeout 回调 this 的构造器:Timeout

在浏览器里同一个回调拿到的是 window(实测 this === window 为 true)。而模块顶层又是另一回事:

console.log(`模块顶层 this === globalThis:${this === globalThis}`);
console.log(`模块顶层 this === module.exports:${this === module.exports}`);

输出:

模块顶层 this === globalThis:false
模块顶层 this === module.exports:true

三张脸总结:

位置默认绑定的 this
浏览器脚本顶层 / 浏览器 setTimeout 回调window
Node 模块顶层(CommonJS)module.exports
Node setTimeout 回调Timeout 对象
严格模式下的直接调用undefined

判据:默认绑定没有「统一的默认值」,它由宿主决定。 所以任何依赖「直接调用时 this 是什么」的代码都不能跨环境 —— 要么显式 .call(),要么用箭头函数锁死外层。


八、一页速查

判定顺序(从上往下,第一个成立即答案)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1 是箭头函数?        → 用创建时外层的 this,本函数不接收 this
2 new f()?           → 新建的实例(能覆盖 bind)
3 f.bind(x) 过的?    → x(只看第一次 bind)
4 f.call(x) / apply? → x
5 a.b.f() / a[i]f()? → 最后一个点/中括号前的那个对象
6 其余(f() / (0,f)() / 解构)→ 默认绑定
                       严格 → undefined
                       非严格 → 宿主决定

同一函数的 11 种调用
────────────────────────────────────────────
who()                globalThis
obj.who()            obj
deep.mid.who()       mid
const f=o.who; f()   globalThis
const {who}=o; who() globalThis
who.call(o)          obj
who.apply(o)         obj
who.bind(o)()        obj
new who()            new(who)
[o.who][0]()         数组本身(不是 globalThis)
(0, o.who)()         globalThis

严格模式改什么、不改什么
────────────────────────────────────────────
改    默认绑定:globalThis → undefined
不改  隐式绑定 a.b.f():仍是 a
不改  显式绑定 call/apply/bind
不改  new 绑定
class 体内默认严格 → 类方法解构后 this 是 undefined

箭头函数丢了什么(一组绑在一起的能力)
────────────────────────────────────────────
this            取外层,call/apply/bind 静默无效
arguments       取外层
[[Construct]]   不能 new → TypeError
prototype       没有这个属性

默认绑定由宿主决定
────────────────────────────────────────────
浏览器顶层 / setTimeout 回调   window
Node 模块顶层                module.exports
Node setTimeout 回调         Timeout 对象
严格模式直接调用              undefined
易错点
────────────────────────────────────────────
「bind 之后 this 就永久锁定了」
   → 对 call/apply 成立,对 new 不成立

「重复 bind 会把 this 换成新的」
   → 静默无效,只认第一次

「箭头函数能用 call 改 this」
   → 不报错,但也没效果(静默失效)

「对象里的箭头函数 this 指向对象」
   → 不指向。对象字面量不是作用域

「严格模式下 this 总是 undefined」
   → 只改默认绑定,方法调用照样绑对象

「[o.f][0]() 是丢了 this」
   → 不是丢,是换成了数组本身

「解构方法报 Cannot read properties of undefined 是参数问题」
   → 是 this 丢了,class 方法默认严格

附:本文的数据来源与验证方法

数据项来源方法
11 种调用形态的 thisNode 22.22.2同一个 who 函数,每种形态独立求值,用引用身份(===)与标记属性判定
new 覆盖 bind同上P.bind(o1) 后分别普通调用与 new,检查 this 身份与 instanceof
bind 只认第一次同上P.bind(o1).bind(o2),比对结果身份
严格模式的作用范围同上同一函数加/不加 'use strict',分别直接调用与作为方法调用
三种取出变体同上[o.f][0]() / (0, o.f)() / (o.f)() 各自求值
箭头函数的四种绑定尝试同上在 outer.call({...}) 里创建箭头,分别直接/call/apply/bind 调用
箭头函数不可 new、无 prototype同上new 捕获 TypeError;'prototype' in fn
class 方法解构同上const detach = c.inc; detach(),捕获错误类型与文案
浏览器侧默认值Chrome 152 headlessCDP Runtime.evaluate 求值:顶层 this === window、setTimeout 回调、事件监听器
Node 顶层的 thisNode 22.22.2CommonJS 模块文件里比对 this === globalThis 与 this === module.exports;另用 node -e 与 vm.runInContext 对照脚本作用域
规范依据ECMA-262[[ThisMode]](lexical / strict / global)三态;OrdinaryCallBindThis;EvaluateCall 对 Reference 的处理(决定隐式绑定);BoundFunctionCreate 的 [[BoundThis]] 与 [[Construct]] 转交;箭头函数的 [[ThisMode]]: lexical

所有断言在 Node 22.22.2 上逐条复跑通过,浏览器侧断言经 CDP 在 Chrome 152 实测。错误文案可能随引擎变化,本文判据落在错误类型与引用身份上。文中标 [浏览器] 的结论给出了两端实测值,其余代码块可直接 node 运行。