Skip to content

Method Shorthand

Method shorthand is a shorter way to put a function in an object literal. Instead of a property whose value is a function expression, you write the method’s name, its parameters and its body, with no colon and no function keyword.

const object = {
methodName(parameter) {
// body
},
};

The two methods below behave the same when called, and both see the object as this:

const basket = {
items: ['bread', 'milk'],
countOld: function () {
return this.items.length;
},
count() {
return this.items.length;
},
};
console.log(basket.countOld());
console.log(basket.count());

This prints:

2
2

The shorthand is the usual way to write methods in object literals today, and it matches the way methods are written in a class body.

An arrow function stored as a property looks like a method, but it has no this of its own. It takes this from the code around the object literal, which is not the object:

const playlist = {
name: 'Road trip',
describeArrow: () => `Playlist: ${this.name}`,
describe() {
return `Playlist: ${this.name}`;
},
};
console.log(playlist.describe());
console.log(playlist.describeArrow());

This prints:

Playlist: Road trip
Playlist: undefined

What this is inside the arrow function depends on where the file runs. At the top level of a Node CommonJS file, as here, it is the empty module.exports object, so this.name is undefined. In a browser’s classic script it is window, and in an ES module it is undefined, so the same call throws a TypeError. In none of these cases is it the object, so write methods with the shorthand and keep arrow functions for callbacks inside them.

The same syntax works with computed names, getters and setters, generators and async methods:

const field = 'total';
const account = {
balance: 40,
[`${field}WithFee`]() {
return this.balance + 2;
},
get overdrawn() {
return this.balance < 0;
},
*months() {
yield 'Jan';
yield 'Feb';
},
};
console.log(account.totalWithFee());
console.log(account.overdrawn);
console.log([...account.months()]);

This prints:

42
false
[ 'Jan', 'Feb' ]

An async method is written async load() { }, and it returns a promise like any async function. Getters & Setters covers get and set.

Shorthand methods are not quite identical to function expressions. They cannot be called with new, because they are not constructors:

const shapes = {
Square: function (side) {
this.side = side;
},
Circle(radius) {
this.radius = radius;
},
};
console.log(new shapes.Square(3));
try {
new shapes.Circle(2);
} catch (error) {
console.log(error.message);
}

This prints:

Square { side: 3 }
shapes.Circle is not a constructor

In return, a shorthand method can use super to call a method on the object’s prototype, which a function expression cannot:

const animal = {
describe() {
return 'an animal';
},
};
const dog = {
__proto__: animal,
describe() {
return `a dog, which is ${super.describe()}`;
},
};
console.log(dog.describe());

This prints:

a dog, which is an animal

__proto__ in an object literal sets the new object’s prototype, and Prototypes explains prototypes in general.

Steve Griffith covers this topic in his video ES6 Method Shorthand.