Skip to content

Creating Objects

JavaScript offers several ways to create an object, and they differ mainly in how easily they make many objects of the same shape, where shared methods are stored and whether any data can be kept private. This page builds the same small object five ways: a counter with a label, a count and an add() method that adds one and returns the new count.

An object literal writes the object out directly. It is the simplest form and the right one for a single object, such as a set of settings:

const visitors = {
label: 'Visitors',
count: 0,
add() {
this.count += 1;
return this.count;
},
};
visitors.add();
console.log(visitors.add(), visitors);

This prints:

2 { label: 'Visitors', count: 2, add: [Function: add] }

The method is an own property of the object, so a second counter written this way would need its own copy of add.

Object.create() makes an object whose prototype is an object you pass in, so the methods can live in one shared place. The optional second argument defines the new object’s properties with descriptors:

const counterMethods = {
add() {
this.count += 1;
return this.count;
},
};
const sales = Object.create(counterMethods, {
label: { value: 'Sales', enumerable: true },
count: { value: 0, writable: true, enumerable: true },
});
sales.add();
console.log(sales.add(), sales);

This prints:

2 { label: 'Sales', count: 2 }

add no longer appears on the object itself, because it is inherited from counterMethods. Descriptor attributes that are left out default to false, which is why count needs writable: true.

A constructor function sets up each new object through this, and the new operator creates the object and links it to the function’s prototype, where shared methods go:

function Counter(label) {
this.label = label;
this.count = 0;
}
Counter.prototype.add = function () {
this.count += 1;
return this.count;
};
const returns = new Counter('Returns');
returns.add();
console.log(returns.add(), returns);

This prints:

2 Counter { label: 'Returns', count: 2 }

This was the usual pattern before classes, and it is still common in older code.

A factory function is an ordinary function that builds and returns an object literal, so it needs no new. A variable declared inside the factory stays available to the returned methods through a closure, but nothing outside can reach it, which makes it private:

function makeCounter(label) {
let count = 0;
return {
label,
add() {
count += 1;
return count;
},
get total() {
return count;
},
};
}
const signUps = makeCounter('Sign-ups');
signUps.add();
console.log(signUps.add(), signUps.total, signUps.count);

This prints:

2 2 undefined

The count can only change through add(), and the getter total reads it. The cost is that every object gets its own copy of each method, because they are created afresh on each call.

A class is the modern syntax for a constructor and its prototype methods in one block. A field whose name starts with # is private to the class, so it gives the same protection as the closure while the methods stay shared on the prototype:

class Counter {
#count = 0;
constructor(label) {
this.label = label;
}
add() {
this.#count += 1;
return this.#count;
}
get total() {
return this.#count;
}
}
const orders = new Counter('Orders');
orders.add();
console.log(orders.add(), orders.total, orders);

This prints:

2 2 Counter { label: 'Orders' }
  • Use an object literal for a one-off object.
  • Use a class when many objects share the same shape and methods, which covers most cases where the older constructor function was used.
  • Use a factory function when you would rather avoid new and this, or when the function may return different kinds of object.
  • Use Object.create() when you want to choose an object’s prototype directly, or need an object with no prototype at all.

Introduction to Classes and Private Variables go further into classes and privacy.

Steve Griffith covers this topic in his video Different Ways of Creating Objects in JavaScript.