October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

JavaScript Objects and Arrow Functions: How `this` Works

An object property does not give an arrow function the object’s this. Learn when to use concise methods and when arrows belong in callbacks.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

An arrow function stored on an object does not make that object its this. To have a function use the object it is called on, write it as a method; use an arrow for a nested callback when it should retain the surrounding this.

Why an arrow function does not get its object’s this

JavaScript object-literal braces do not create a new this scope. An arrow function gets its this from the surrounding lexical context, not from the object that stores it. So calling an arrow-valued property through an object does not change its this.

const user = {
  name: "Ari",
  greet: () => this.name,
};

user.greet(); // Does not read user.name

The arrow captures the surrounding this where the object literal is evaluated. Depending on that context, this.name may be undefined or refer to something else; it is not automatically user.name. MDN explains that [object literals do not create a this scope](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this).

Use a method when this should be the calling object

With concise method syntax, a call such as user.greet() supplies user as the receiver. The method’s ordinary-function this is determined when it is called.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const user = {
  name: "Ari",
  greet() {
    return this.name;
  },
};

user.greet(); // "Ari"

This receiver-based behavior depends on how the function is invoked. Calling the same ordinary function without an object receiver can produce a different this, depending on strictness and execution context. See MDN’s explanation of [how JavaScript determines this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this).

Choose the function form for its intended role

Function form How this is selected Good fit
Concise object method, such as greet() { ... } At call time, from the receiver before the dot Behavior that should operate on the object it is called on
Arrow-valued property, such as greet: () => { ... } Lexically, from the surrounding context; not from the call receiver A callback or function that should retain an enclosing this, when appropriate

An arrow is not simply a shorter replacement for every function. For an object method, use concise method syntax if its behavior should depend on the receiver. For a callback nested inside such a method, an arrow can preserve that method invocation’s this. MDN’s arrow-function reference covers both patterns.

Use arrows for callbacks that should retain an outer this

An arrow callback does not establish its own this, so it can use the surrounding method’s receiver. For example, increment() below is called as counter.increment(); the arrow passed to setTimeout retains that invocation’s this.

const counter = {
  count: 0,
  increment() {
    this.count++;
    setTimeout(() => {
      console.log(this.count);
    }, 0);
  },
};

counter.increment();

The same reasoning can apply to callbacks passed to methods such as map or filter, and to promise handlers: choose an arrow when the callback should use the surrounding this. If the callback needs its own receiver-based this, an arrow is not the right form.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What call, apply, and bind do to arrow functions

For an ordinary function, call, apply, and bind can supply or set its this. They cannot replace an arrow function’s lexical this. Calling an arrow through another object likewise does not give it that object as its receiver. Use a regular function or method if you need this to be set by invocation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Two other arrow-function limits

  • No own arguments binding: an arrow uses the surrounding function’s arguments, if one exists. Use a rest parameter, such as (...args) => {}, to collect the arrow’s arguments.
  • Not a constructor: an arrow function cannot be called with new. Use a constructible function form when instances should be created with new.

For broader reference, MDN’s JavaScript functions guide provides additional context on function forms and behavior.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.