There are multiple ways to create an objects in javascript.
1. Object Literals: This is the simplest way to create an object in javascript
const obj = {
name: "John",
age: 30,
greet: function(){
console.log("Object Literal Example")
}
}
2. Using Object Constructor: Create an Object using the Object Constructor
const obj = new object();
obj.name = "john";
obj.age = 30;
obj.greet = function(){
console.log("Object Creation using Object Constructor");
}
3. Using Constructor Function:
Define a custom constructor function and use it to create an objects
function person(name, age){
this.name= name;
this.age = age;
this.greet = function(){
console.log(`Hello this object creation using constructor function ${this.name}`);
};
}
const john = new Person("John", 30)
Top comments (1)
good