Creating Objects in JavaScript: A Detailed Guide


In JavaScript, objects are used to store collections of data and methods. There are multiple ways to create objects in JavaScript, each with its own advantages and disadvantages depending on the scenario.

1.Using object literals:

This is the most common and simplest way to create an object. Object literals are enclosed in curly braces {} and consist of a set of key-value pairs. Here is an example:

const person = {
    name: "John",
    age: 30,
    job: "Developer"
};
console.log(person);
2.Using the object constructor:

The object constructor is a built-in function in JavaScript, which is used to create an object prototype. Here is an example:

const person = new Object();
person.name = "John";
person.age = 30;
person.job = "Developer";
console.log(person);
3. Using the Object.create() method:

The Object.create() method is used to create an object with a specific prototype. example:

const personProto = {
sayHello: function() {
console.log(`Hello, my name is ${this.name}`);
    }
};
const person = Object.create(personProto);
person.name = "John";
person.age = 30;
person.job = "Developer";
console.log(person);

In the above code snippet, we are creating an object called "personProto" that contains a single method "sayHello()". This method takes advantage of the "this" keyword to reference the "name" property of the object it is called on.

Next, we use the Object.create() method to create a new object called "person" that is based on the "personProto" object. This is known as object prototypal inheritance.

We then add properties to the "person" object, such as "name", "age", and "job". These properties are specific to this object and are not present on the "personProto" object.

Finally, we log the "person" object to the console, which will show all of its properties and methods.

One of the advantages of using the Object.create() method is that it allows for efficient object inheritance and property addition without the need for a constructor function and the "new" keyword. Additionally, it allows for easy separation of methods and properties shared among objects and those that are specific to an individual object.

However, one of the disadvantages of using Object.create() is that it is not supported in all older browsers. So, If you are planning to create a web application, you need to ensure that your application works on all browsers and platforms.

In conclusion, the Object.create() method is a powerful tool for creating objects in JavaScript and allows for efficient object inheritance and property addition. However, it is important to be aware of its browser support limitations when building web applications.

4.Using class:

The class is the latest addition to creating objects in JavaScript. example:

class Person {
constructor(name, age, job) {
        this.name = name;
this.age = age;
this.job = job;
    }
}
const person = new Person("Tiya", 30, "Developer");
console.log(person);

This code creates a class called "Person" using the class keyword. The class has a constructor method which is called when a new object of the class is created using the "new" keyword. The constructor takes 3 parameters: name, age, and job. Inside the constructor method, the "this" keyword is used to create properties on the object being created, and set their values to the values of the arguments passed to the constructor method.

In this example, a new object of the class "Person" is created and assigned to the variable "person". The values "Tiya", 30, and "Developer" are passed as arguments to the constructor method. This causes the properties "name", "age" and "job" to be created on the "person" object, and their values set to "Tiya", 30, and "Developer" respectively.

At the end of the code, the "console.log(person)" is used to log the object to the console. This will display the object with its properties and values.

Once an object is created, you can add properties and methods to it, and access them using dot notation (person.name) or bracket notation (person["name"]).

Additionally, you can use destructuring to extract data from an object. Here is an example:

const {name,age,job} = person;
console.log(name);

In conclusion, the choice of creating an object will depend on the specific requirements of your application. Each method has its own advantages and disadvantages and it's important to understand them to make the best use of them.

List of Programs


JS Practical Questions & Answers


JS Practical Project