大家有空帮我看下这个问题哈,不胜感激
代码如下:
var userName = "zhangsan";
var person = {
userName: "lisi",
method: function () {
return function () {
return this.userName;
};
},
};
console.log(person.method()());
我的问题:
return function () {
return this.userName;
};
盼复
函数内部的this指向,取决于函数的调用方式。
person.method()() 执行过程等价于:
let func = person.method(); // 此时,method作为person对象方法调用,所以this指向person。
func(); // 此时func()是在全局执行环境调用的,this指向顶层对象window或global。
正在学习这个,以下是对this的理解:
JavaScript中的this是在运行时绑定的。在不同的执行上下文中,this指向是不同的:
百度搜索 js this用法 应有尽有