DEV Community

Cover image for Regular this, Arrow this (Tersely bulleted)
Dhokia Rinidh
Dhokia Rinidh

Posted on

1

Regular this, Arrow this (Tersely bulleted)

Hello code-mates, ever since I learnt about this in Javascript a year ago, I had never fully felt mentally confident that I can interpret, analyse and refactor complicated scenarios where this is used. I therefore made a small cheatsheet-like summary to always help me get the immediate context & idea, especially when I am rushing through documentations or others’ code, and obviously infiltrate well later.

Here are a few bullets I always find handy in relation to this:

  • All regular functions (function myFunction() {}) always have their this as the global window object (in browsers) or global object (in Node JS), UNLESS it is called in a different context:
    • As a method of an object.
    • Explicitly bound by .bind(), .call() or .apply().
  • Regular functions have to be explicitly bound with a this value while arrow functions (() => {}) automatically get their this from the immediate enclosing parent-function (lexical scope).
  • The lexical/enclosing scope in which an arrow function is actually running is the immediate parent REGULAR function, even though it is inside an object’s method.
  • The this value of arrow functions (got at time of creation) is unchangeable while that of regular functions is changeable (.bind(), .call() etc.)
  • Unlike methods of object literals (const obj = {...}), functions defined as methods of classes, Event though regular or arrow, their this is always the object instance created (new MyClass())

Please feel free to suggest edits, add more bullets, or even criticize me on any blunders. I always welcome an opportunity to learn from excellent programmers like you! ☺️ 💛

$150K MiniMax AI Agent Challenge — Build Smarter, Remix Bolder, Win Bigger!

Join the MiniMax AI Agent Challenge — Build your first AI Agent 🤖

Developers, innovators, and AI tinkerers, build your AI Agent and win $150,000 in cash. 💰

Read more →

Top comments (0)

Short-term memory for faster AI agents

Short-term memory for faster AI agents

AI agents struggle with latency and context switching. Redis fixes it with a fast, in-memory layer for short-term context—plus native support for vectors and semi-structured data to keep real-time workflows on track.

Start building

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay