面试题答案
一键面试输出结果原因解释
new outer()
:通过new
关键字调用outer
函数,会创建一个新的对象(outer
函数的实例),此时outer
函数内部的this
指向这个新创建的实例对象。let self = this;
:将outer
函数内部的this
(即新创建的实例对象)赋值给self
变量。function middle()
:middle
函数是在outer
函数内部定义的普通函数,它本身没有自己的this
绑定(在非严格模式下,默认this
指向全局对象window
;在严格模式下,this
为undefined
)。function inner()
:inner
函数同样是普通函数,它也没有自己的this
绑定,在inner
函数中执行console.log(this === self);
时,this
指向全局对象window
(非严格模式)或undefined
(严格模式),而self
指向outer
函数的实例,所以this === self
返回false
。
修改代码使 inner
函数中的 this
指向 outer
函数的实例
- 使用箭头函数:箭头函数没有自己的
this
绑定,它的this
继承自外层作用域。可以将inner
函数改为箭头函数:
function outer() {
let self = this;
function middle() {
const inner = () => {
console.log(this === self);
};
inner();
}
middle();
}
new outer();
在上述代码中,箭头函数 inner
的 this
继承自 middle
函数的作用域,而 middle
函数的作用域中 this
虽然不是指向 outer
实例,但箭头函数会沿着作用域链找到 outer
函数作用域中的 this
(即 outer
实例),此时 this === self
返回 true
。
- 使用
bind
方法:在调用inner
函数时,可以使用bind
方法将this
绑定到outer
函数的实例上:
function outer() {
let self = this;
function middle() {
function inner() {
console.log(this === self);
}
inner.bind(this)();
}
middle();
}
new outer();
这里 inner.bind(this)()
会创建一个新的函数,并且将这个新函数的 this
绑定到 middle
函数调用时的 this
,而 middle
函数在 outer
函数内部调用,outer
函数通过 new
调用,所以 middle
函数调用时的 this
指向 outer
函数的实例,这样在 inner
函数中 this === self
返回 true
。