You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
this in register event function is pointing to the attached element, but in this example
letbtn=document.querySelector('button');btn.onclick=function(){console.log(this)};//click the button to call the function
5. Timer function
this is pointing to Window
setInterval(function(){},1000);//execute function in every second
6. IIFE (Immediately Invoked Function Expression)
this is pointing to Window
(function(){console.log('This is a function')})();
Understanding of 'this' keyword
Scenario 1: 'this' in global environment
It is relatively straightforward in this scenario, the browser in the global environment invokes the function. 'this' is pointing to ** Window**. However, 'this' will point to undefined if 'use strict'.
this is still pointing to the Window. Although function fn is invoked by the object foo (foo.fn), after assignment to fn1, there is a (). That means we execute fn1 in the global environment. Therefore, the code above still outputs window and undefined.
this is pointing to the object that invokes the function. Whenever a preceding dot calls a function, the object before that dot is this.
To be more clear, in foo.fn(), this is pointing to foo. When executing function, if this in the function is invoked by parent scope, then this will point to parent scope environment, otherwise, this point to global environment
Why? Because in these nested relationships, this will point to the LAST object that invokes the function. Here is the brother scope, so we choose the name variable that is in brother scope.
We don't execute o1.fn, instead, we only need the this.text inside o1.fn function. So in o2, we put this.text on o2.fn. Then execute it in the console.log(o2.fn())
It will output Lucas, which means instance is the object instantiating this
Conclusion: If a value is explicitly returned in the constructor and an object is returned, then this points to the returned object; if the returned object is not an object, then this still points to the instance.
1. During function precompilation, `this` points to `window`
2. In the global scope, `this` points to `window`
3. call/apply can change `this` pointing
4. obj.fn() who calls the method `this` points to whom
Scenario 5: New story, this in the arrow function
this for arrow functions does not apply to the above standard rules, but is determined according to the outer (function or global) context scope.
In this question, this is in an anonymous function of setTimeout(), so this is pointing to window object. However, we can use arrow functions to make this points to object foo
We often call the binding of this through call, apply, bind, and new as explicit binding; the pointing of this determined according to the call relationship is called implicit binding.
But which one has a higher priority?
The output will be 2. We first bind this in foo() on obj1. The this of bar (reference arrow function) will also be bound to obj1, and the arrow function binding cannot be modified. Note here, foo() is not an arrow function, that's why we can use call function on foo.
1. Regular function
this
is pointing to Window2. Method on objects
this
is pointing to object => obecause
o
calledsayHI
method3. Constructor
this
is pointing torick
becauserick
instantiate the constructorthis
in the prototype object is also pointing torick
4. Register Event function
this
in register event function is pointing to the attached element, but in this example5. Timer function
this
is pointing to Window6. IIFE (Immediately Invoked Function Expression)
this
is pointing to WindowUnderstanding of 'this' keyword
Scenario 1: 'this' in global environment
It is relatively straightforward in this scenario, the browser in the global environment invokes the function. 'this' is pointing to ** Window**. However, 'this' will point to undefined if 'use strict'.
There is a very similar question (tedious, useless, but interesting)
Output:
this
is still pointing to the Window. Although functionfn
is invoked by the objectfoo
(foo.fn), after assignment to fn1, there is a (). That means we execute fn1 in the global environment. Therefore, the code above still outputs window and undefined.Now, if we change to
Output
this
is pointing to the object that invokes the function. Whenever a preceding dot calls a function, the object before that dot is this.foo.fn()
,this
is pointing tofoo
. When executing function, ifthis
in the function is invoked by parent scope, thenthis
will point to parent scope environment, otherwise,this
point to global environmentScenario 2: 'this' in the context environment
Let's dive into examples.
Why? Because in these nested relationships,
this
will point to the LAST object that invokes the function. Here is thebrother
scope, so we choose thename
variable that is inbrother
scope.Example 2:
WHAT???
Let's dive into it:
o1
because ofo1.fn()
,o1
calledfn()
so this.text will print the variable that is insideo1
object.o2
that invokedfn()
, inside the function of o2, it iso1.fn()
. Surprisingly, we jump to o1 and geto1
as its inputvar fn
is assigned, it is a normalfn()
call, so here this points to the Window, and the answer is, of course, undefined.A follow-up question, if we want
o2
as output forconsole.log(o2.fn())
without using call, apply, bind
What should we do?
We don't execute
o1.fn
, instead, we only need thethis.text
insideo1.fn
function. So in o2, we putthis.text
ono2.fn
. Then execute it in theconsole.log(o2.fn())
Scenario 3: bind / call / apply
The above code will output
mike
Scenario 4: this and constructor
The output will be
Lucas
, but what happens behind the scene afternew
with constructorFoo
?this
in constructor will point to the new objectThe above can be shown as code below
Here, we only simulate a naive version of
new
Note, if
return
has been mentioned in a constructor, there are two cases needed to be consideredFirst
It will output
undefined
becauseinstance
is an empty objecto
just returnedSecond
It will output
Lucas
, which meansinstance
is the object instantiatingthis
Conclusion: If a value is explicitly returned in the constructor and an object is returned, then
this
points to the returned object; if the returned object is not an object, thenthis
still points to the instance.Scenario 5: New story,
this
in the arrow functionthis
for arrow functions does not apply to the above standard rules, but is determined according to the outer (function or global) context scope.In this question,
this
is in an anonymous function ofsetTimeout()
, sothis
is pointing towindow
object. However, we can use arrow functions to makethis
points to objectfoo
Scenario 6: Prority of
this
We often call the binding of this through
call
,apply
,bind
, andnew
as explicit binding; the pointing ofthis
determined according to the call relationship is called implicit binding.But which one has a higher priority?
Without
call
function, the output will be1
and2
.Now the output is
2
and1
which means
call
,apply
has a higher priority.By using
bind
,this
inbar
has been binded onobj1
. After executingbar(2)
,obj1.a = 2
. Which is, afterbar(2)
,obj1
is `{a: 2}Turn to arrow function
The output will be 2. We first bind
this
infoo()
onobj1
. Thethis
ofbar
(reference arrow function) will also be bound toobj1
, and the arrow function binding cannot be modified. Note here, foo() is not an arrow function, that's why we can usecall
function on foo.If we refactor
foo
to arrow function completelyIt will output
123
.Reference
https://www.zhihu.com/question/353757734/answer/964557747
https://blog.kevinchisholm.com/javascript/context-object-literals/
The text was updated successfully, but these errors were encountered: