We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
问题:
function Person(){ (()=> {console.log(this)})(); } Person(); //window new Person();//Person{}.
为何Person()指向window,new Person()指向Person对象?
根据箭头函数的this指向定义时的上下文的this
Person()
ECStack = [ //执行上下文栈 PersonContext, globalContext ]; globalContext = { //全局上下文 VO: [global], Scope: [globalContext.VO], this: globalContext.VO } PersonContext = { //Person上下文 AO: { //活动对象 arguments: { length: 0 }, scope: undefined, f: reference to function f(){} }, Scope: [AO, globalContext.VO], //作用域,包括Person活动对象和全局对象 this: undefined //重点,因为没有new,所以this是undefined }
定义时的上下文是PersonContext,但是由于PersonContext的this没有定义,因此采用上一级也就是globalContext的this,即window。由于箭头函数的this指向定义时的上下文的this,因此箭头函数的this指向window。
new Person()
ECStack = [ //执行上下文栈 PersonContext, globalContext ]; globalContext = { //全局上下文 VO: [global], Scope: [globalContext.VO], this: globalContext.VO } PersonContext = { //Person上下文 AO: { //活动对象 arguments: { length: 0 }, scope: undefined, f: reference to function f(){} }, Scope: [AO, globalContext.VO], //作用域,包括Person活动对象和全局对象 this: Person //重点,new,所以this是Person }
定义时的上下文是PersonContext,PersonContext的this指向Person实例。由于箭头函数的this指向定义时的上下文的this,因此箭头函数的this指向Person实例。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
问题:
为何Person()指向window,new Person()指向Person对象?
根据箭头函数的this指向定义时的上下文的this
Person()
定义时的上下文是PersonContext,但是由于PersonContext的this没有定义,因此采用上一级也就是globalContext的this,即window。由于箭头函数的this指向定义时的上下文的this,因此箭头函数的this指向window。
new Person()
定义时的上下文是PersonContext,PersonContext的this指向Person实例。由于箭头函数的this指向定义时的上下文的this,因此箭头函数的this指向Person实例。
The text was updated successfully, but these errors were encountered: