Using object constructors to create objects in JavaScript

Posted by

Creating Objects in JavaScript Using Object Constructors

Objects are an important part of the JavaScript language. They allow us to store and manipulate data in a structured way. In this tutorial, we will learn how to create objects in JavaScript using object constructors.

What is an Object Constructor?

An object constructor is a function that is used to create an object. To create an object, we first define the object constructor function and then use the new keyword to instantiate the object. The object constructor function has the same name as the object it creates and is used to set the initial values of the object’s properties.

Defining an Object Constructor

To define an object constructor, we use the function keyword followed by the name of the object. Inside the function, we set the initial values of the object’s properties. We can also add methods to the object constructor. Methods allow us to manipulate the object’s data.

Here is an example of an object constructor for a person object:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

function Person(name, age) {
  this.name = name;
  this.age = age;
  this.getInfo = function() {
    return this.name + ' is ' + this.age + ' years old';
  }
}

[/dm_code_snippet]

In this example, we are creating a Person object with two properties name and age. We also have a method getInfo that returns a string containing the person’s name and age.

Creating an Object Instance

Once we have defined the object constructor, we can use the new keyword to create an instance of the object. This is called instantiation. To instantiate an object, we pass the necessary arguments to the object constructor.

Here is an example of instantiating a Person object:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var person1 = new Person('John', 30);

[/dm_code_snippet]

In this example, we are creating a Person object with the name John and age 30.

Accessing Object Properties and Methods

Once we have created an object instance, we can access its properties and methods using the dot notation. To access the object’s properties, we use the object.property syntax. To access the object’s methods, we use the object.method() syntax.

Here is an example of accessing the Person object’s properties and methods:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

console.log(person1.name); // John
console.log(person1.age); // 30
console.log(person1.getInfo()); // John is 30 years old

[/dm_code_snippet]

In this example, we are accessing the Person object’s name, age properties and getInfo method.

Conclusion

In this tutorial, we learned how to create objects in JavaScript using object constructors. We discussed how to define an object constructor and how to instantiate an object instance. We also learned how to access the object’s properties and methods.