Create an Objects in JavaScript (5 Programs)
Creating objects is an essential part of JavaScript programming. It helps developers organize and work with data effectively. This tutorial explains several simple and advanced ways to create objects, making it easier to handle various coding challenges. By learning these techniques, you’ll be able to build strong and adaptable JavaScript applications.
Create an Objects in JavaScript Using Object Literals
The simplest and most common way to create objects in JavaScript is using object literals.
Code
const person = {
name: "John",
age: 30,
greet: function() {
console.log(`Hello, my name is ${this.name}.`);
}
};
// Accessing properties and methods
console.log(person.name);
person.greet();
Output
John
Hello, my name is John.
Explanation
-
Object literals are defined with curly braces {}.
-
Properties and methods are assigned directly within the braces.
-
This method is concise and ideal for creating small objects.
Create an Object in JS using new Object()
This approach uses the built-in Object constructor to create objects.
Code
const car = new Object();
car.make = "Toyota";
car.model = "Corolla";
car.drive = function() {
console.log(`Driving a ${this.make} ${this.model}.`);
};
// Accessing properties and methods
console.log(car.make);
car.drive();
Output
Toyota
Driving a Toyota Corolla.
Explanation
-
The new Object() syntax creates an empty object.
-
Properties and methods are added dynamically.
-
This method is less common but useful for creating objects programmatically.
Creating Object in JavaScript Using Object Constructor Functions
Constructor functions allow you to create multiple objects with the same structure.
Code
function Animal(type, sound) {
this.type = type;
this.sound = sound;
this.speak = function() {
console.log(`${this.type} says ${this.sound}`);
};
}
const dog = new Animal("Dog", "Woof");
dog.speak();
const cat = new Animal("Cat", "Meow");
cat.speak();
Output
Dog says Woof
Cat says Meow
Explanation
-
Constructor functions use the function keyword and are invoked with new.
-
Each created object has its own properties and methods.
JavaScript Program to Create an Object Using ES6 Classes
Classes in ES6 provide a cleaner and more modern way to create objects.
Code
class Book {
constructor(title, author) {
this.title = title;
this.author = author;
}
details() {
console.log(`${this.title} by ${this.author}`);
}
}
const book1 = new Book("1984", "George Orwell");
book1.details();
Output
1984 by George Orwell
Explanation
-
Classes use the class keyword and include a constructor method.
-
Methods are defined directly inside the class.
-
ES6 classes simplify object creation and inheritance.
JavaScript Program to Create an Object Using Object.create()
The Object.create() method creates a new object with the specified prototype.
Code
const prototypeAnimal = {
speak: function() {
console.log(`${this.type} makes a noise.`);
}
};
const lion = Object.create(prototypeAnimal);
lion.type = "Lion";
lion.speak();
Output
Lion makes a noise.
Explanation
-
Object.create() creates an object linked to a specified prototype.
-
This method is useful for implementing inheritance.
Content Used in the Programs
-
Object Literals: Quick and efficient for defining static objects.
-
new Object(): Creates objects dynamically with the constructor.
-
Constructor Functions: Reusable templates for object creation.
-
ES6 Classes: Modern, clean syntax for creating and extending objects.
- Object.create(): Flexible for creating objects with specified prototypes.