JavaScriptQuestion 19 of 36
What are the ways to create an Object in JavaScript?
Answer out loud or in your head first, then compare.
Answer
Literal:
JavaScript
const user = {
name: "Tom"
};Constructor function:
JavaScript
function User(name) {
this.name = name;
}
const user = new User("Tom");Class:
JavaScript
class User {
constructor(name) {
this.name = name;
}
}Factory:
JavaScript
function createUser(name) {
return { name };
}Object.create:
JavaScript
const obj =
Object.create(proto);How did it go? Be honest — “Not yet” puts the question on your review list.