How do you create an object?
By FrontendPro Editorial Team Updated 9/26/2026
Answer
// 1. Literal — what you want almost always
const a = { name: "Mai", greet() { return "hi"; } };
// 2. Object.create — choose the prototype explicitly
const b = Object.create(protoObject);
const dict = Object.create(null); // no prototype at all
// 3. Class
class Person { constructor(name) { this.name = name; } }
const c = new Person("Mai");
// 4. Constructor function — the pre-class form
function P(name) { this.name = name; }
const d = new P("Mai");
// 5. Factory function — returns an object, no `new`
const makeUser = (name) => ({ name, greet: () => "hi" });
const e = makeUser("Mai");
// 6. new Object() — no reason to use this
const f = new Object();
Which to use
The literal for plain data. A class when you need many instances sharing methods, inheritance, or instanceof. A factory when you want closures for private state and no this at all — it cannot be called wrongly, because there is no new to forget.
Object.create(null) deserves a mention
It produces an object with no prototype, so there is no inherited toString, constructor or __proto__. That makes it the safe choice for a dictionary keyed by user input, where a key named __proto__ would otherwise cause trouble. A Map is usually better still.
Class vs factory
A class puts methods on the prototype — one copy shared by every instance, and cheap. A factory creates new function objects per call, which costs memory at scale but gives you genuinely private variables through closure.
Both are fine. Choose a class when you want inheritance and instanceof; choose a factory when you want privacy and to avoid this entirely.
Copying an existing object
{ ...source } for a shallow copy, structuredClone(source) for a deep one.
