跳到主内容
← 返回题库
八股概念3/5 中等前端 · 一面 ★ 0 · 浏览 3

JS 中 this 的绑定规则与箭头函数差异

阅读代码并说明各处 this 指向:const obj={name:‘o’,fn(){ console.log(this.name); const inner=()=>console.log(this.name); const normal=function(){console.log(this.name)}; inner(); normal(); setTimeout(()=>console.log(this.name),0) }}; obj.fn(); 再问:obj.fn.call({name:‘x’}) 时箭头函数里的 this 是否变化?总结 this 绑定优先级,并说明 class 方法 this 丢失的常见场景与修复方式。

参考答案

先自己想 30 秒再看答案,比直接读一遍有用得多。

追问链

  1. 1. 严格模式与非严格模式对默认绑定的差异;React 类组件为什么常需要 bind