白狗子的博客

「天上浮云如白衣,斯须改变如苍狗。」——杜甫

JavaScript-面试-this与箭头函数

2026年10月16日·Admin·2 次浏览

面试题:this 指向?箭头函数为什么不能当构造函数?

this 绑定规则(优先级从高到低)

  1. new 调用:this 指向新创建的对象。
  2. call / apply / bind:显式指定 this(bind 返回绑定后的函数)。
  3. 对象方法调用:obj.fn() 里 this 一般为 obj(注意赋值后裸调会丢)。
  4. 严格模式普通函数调用:this 为 undefined;非严格为全局对象(浏览器里 window)。

易错点

const fn = obj.method;
fn(); // 不是 obj 调用 → this 丢失

类的方法若作为回调传递,常需 bind 或箭头函数包装。

箭头函数

  • 没有自己的 this,词法上继承外层作用域的 this。
  • 没有 prototype,不能 new。
  • 没有 arguments 对象(可用剩余参数 ...args)。
  • 不适合做原型方法若依赖「调用者」动态 this(如 DOM 监听里需要元素作 this 时要小心)。

口述模板

「this 看谁怎么调用;箭头函数绑词法 this,不能当构造器。」

© 2025 My Blog. Built with Nuxt.js v4