Creating an object
Object literal - the simplest and most common way
const user = {
name: 'Oleh',
age: 25,
isAdmin: false
};This is the most readable and most widely used way to create objects.
You can add and remove properties dynamically:
user.city = 'Kyiv';
delete user.isAdmin;2. Via the new Object() constructor
const user = new Object();
user.name = 'Oleh';
user.age = 25;Does the same thing as
{}, but is longer and looks "old-fashioned". Nowadays it is almost never used.
3. A constructor function
Before class existed, this is how objects were created "from a template".
function User(name, age) {
this.name = name;
this.age = age;
}
const oleh = new User('Oleh', 25);
const alex = new User('Alex', 30);
console.log(oleh.name); // "Oleh"When you call a function with
new, JS does the following under the hood:
- creates an empty object
{},- assigns it to
this,- returns that object.
4. ES6 class (syntactic sugar over a constructor function)
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHi() {
console.log(`Hello, I am ${this.name}`);
}
}
const oleh = new User('Oleh', 25);
oleh.sayHi(); // "Hello, I am Oleh"Under the hood,
classis the same function, but with a more convenient and understandable syntax.
5. A factory function
Instead of class or new, you can simply return an object:
function createUser(name, age) {
return {
name,
age,
sayHi() {
console.log(`Hello, ${this.name}`);
}
};
}
const oleh = createUser('Oleh', 25);
oleh.sayHi(); // "Hello, Oleh"A very convenient approach, especially when you do not need
prototypeand inheritance.
6. Object.create()
Creates a new object with the specified prototype.
const person = {
greet() {
console.log(`Hello, I am ${this.name}`);
}
};
const user = Object.create(person);
user.name = 'Oleh';
user.greet(); // "Hello, I am Oleh"Used when you need to explicitly set the prototype chain.
7. Via JSON
You can create an object from a JSON string:
const json = '{"name": "Oleh", "age": 25}';
const user = JSON.parse(json);
console.log(user.name); // "Oleh"Convenient when receiving data from a server. But it only works with simple data types (no methods).
8. Via Object.assign()
Creates an object based on others (copies properties):
const base = { a: 1 };
const extra = { b: 2 };
const result = Object.assign({}, base, extra);
console.log(result); // { a: 1, b: 2 }Can be used for cloning or merging objects.
9. Via the spread operator { ...obj } (ES6)
const user = { name: 'Oleh', age: 25 };
const copy = { ...user, city: 'Kyiv' };
console.log(copy); // { name: 'Oleh', age: 25, city: 'Kyiv' }The most modern way to copy and merge objects.
Additionally: Object.fromEntries()
Creates an object from an array of [key, value] pairs.
const entries = [['name', 'Oleh'], ['age', 25]];
const user = Object.fromEntries(entries);
console.log(user); // { name: 'Oleh', age: 25 }Summary
| Way | Example | Features |
|---|---|---|
| Object literal | { name: 'Oleh' } | the most common way |
| new Object() | new Object() | rarely used |
| Constructor function | function User() { this.name = ... } | an old alternative to class |
| class | class User { ... } | a modern OOP approach |
| Factory function | function createUser() { return {...} } | no this, just returns an object |
| Object.create(proto) | Object.create(person) | creates an object with a given prototype |
| JSON.parse() | JSON.parse('{"a":1}') | creates an object from a string |
| Object.assign() | Object.assign({}, obj1, obj2) | copies/merges objects |
| Spread operator | { ...obj1, ...obj2 } | a short copying syntax |
| Object.fromEntries() | Object.fromEntries([['a',1]]) | from an array of pairs into an object |
In short
The most popular way ->
{}For inheritance ->Object.create()For templates ->classor a constructor function For copying ->{ ...obj }orObject.assign()
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.