Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this问题 #2

Open
brucefuqiming opened this issue Apr 22, 2022 · 1 comment
Open

this问题 #2

brucefuqiming opened this issue Apr 22, 2022 · 1 comment

Comments

@brucefuqiming
Copy link

let name = 2222;
let obj = {
    name: 3333,
    a: function() {
        console.log(this.name); // 2222
    }
}
let func = obj.a;
func(); 

这里有问题,func()执行后,打印的是空字符串或undefined(注意:name变量是window对象上的保留变量,所以window.name默认是空字符串),并不是2222。

原因是name是let声明的,因为obj.a赋值给func执行的时候,this指向的是window,而let声明的对象并不存在于window上,所以输出为undefined。除非用var声明,才能达到本例描述的效果。

@wsydxiangwang
Copy link
Owner

哇去,你不说,我还真不知道呢,扑街

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants