This question has already been answered:
- Loss of context call 5 responses
'use strict' class A { constructor(){} Do(){ alert(this) } } let a = new A() a.Do() let b = a.Do b()
Why does the second call display undefined?
This question has already been answered:
'use strict' class A { constructor(){} Do(){ alert(this) } } let a = new A() a.Do() let b = a.Do b()
Why does the second call display undefined?
A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .
Because it uses "use strict"
When using "use strict" - this
is the default - undefined
In the first call this
- points to the object.
But after receiving the reference to the function and performing
let b = a.Do; b();
The context is changed to a global object, or, when using "use strict" to undefined
Also in mdn
Body class definitions, and
class expressions
are run in strict mode.
this
depends on how exactly the function was called - GrundySource: https://ru.stackoverflow.com/questions/505254/
All Articles