Home > Web Front-end > JS Tutorial > Regular this, Arrow this (Tersely bulleted)

Regular this, Arrow this (Tersely bulleted)

Susan Sarandon
Release: 2025-01-23 04:30:09
Original
450 people have browsed it

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:

  • Regular Functions: In browsers, a regular function (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:
    • As an object method.
    • Explicitly bound using .bind(), .call(), or .apply().
  • Arrow Functions: Arrow functions (() => {}) inherit their this from their surrounding (enclosing) regular function's scope (lexical this). This is crucial to understand.
  • Lexical Scope (Arrow Functions): The 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.
  • Immutability of this: Arrow functions have a fixed, immutable this, while regular functions' this is dynamic and can be altered using binding methods.
  • Class Methods: In class methods (whether regular or arrow), the 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template