Home >Web Front-end >JS Tutorial >Regular this, Arrow this (Tersely bulleted)
Fellow developers, I've found JavaScript's this
keyword a bit tricky, especially in complex scenarios. To improve my understanding and quickly grasp its behavior in various contexts (like when reviewing code or documentation), I created this concise reference.
Key points to remember about this
:
function myFunction() {}
) typically has its this
set to the global window
object. In Node.js, it's the global object. This changes only when called within a specific context:.bind()
, .call()
, or .apply()
.() => {}
) inherit their this
from their surrounding (enclosing) regular function's scope (lexical this
). This is crucial to understand.this
of an arrow function is determined at the time of its creation, not execution. Even if it's inside an object method, its this
comes from the nearest enclosing regular function.this
: Arrow functions have a fixed, immutable this
, while regular functions' this
is dynamic and can be altered using binding methods.this
always refers to the instance of the class created using new MyClass()
. This is consistent regardless of function type.Feel free to contribute, correct any inaccuracies, or add further insights. Constructive feedback is always appreciated!
The above is the detailed content of Regular this, Arrow this (Tersely bulleted). For more information, please follow other related articles on the PHP Chinese website!