Creating Objects in JavaScript
JavaScript objects are used to store data in a structured manner. An object is a collection of related data and/or functionality, and is a data type in JavaScript. JavaScript objects can be created in several ways, including using object literals, the new keyword, and the Object.create() method.
Object Literals
Object literals are a simple and efficient way to create JavaScript objects. A literal is a set of comma-separated name/value pairs, enclosed in curly braces. Here’s an example of an object literal:
[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 person = {
firstName: 'John',
lastName: 'Doe',
age: 30
};
[/dm_code_snippet]
Each name/value pair is known as a property of the object. The property names and values can be of any valid JavaScript type, including strings, numbers, functions, and even other objects. You can access, add, and modify properties of an object literal using the dot notation, like so:
[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(person.firstName); // 'John'
person.job = 'Engineer';
person.age = 32;
[/dm_code_snippet]
Object literals are a great way to create an object with a fixed set of properties. However, if you want to create many objects with the same properties and methods, it is more efficient to use a constructor function.
Constructor Functions
A constructor function is a special type of function used to create objects. The function is invoked with the new
keyword, and the object is created just like an object literal, with the addition of a this
keyword. Here’s an example of a constructor function:
[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(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
[/dm_code_snippet]
You can create an object using this constructor function like so:
[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 john = new Person('John', 'Doe', 30);
console.log(john); // Person { firstName: 'John', lastName: 'Doe', age: 30 }
[/dm_code_snippet]
You can also add methods to constructor functions. Here’s an example of a constructor function with a method:
[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(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.getFullName = function() {
return this.firstName + ' ' + this.lastName;
}
}
var john = new Person('John', 'Doe', 30);
console.log(john.getFullName()); // 'John Doe'
[/dm_code_snippet]
This allows you to create many objects with the same properties and methods. This can be more efficient than creating objects with object literals, as the methods are only defined once in the constructor function, instead of in every object literal.
Object.create() Method
The Object.create()
method is a more recent addition to JavaScript, and is used to create an object from a prototype. The prototype can be an object literal, a constructor function, or another object. Here’s an example of using the Object.create()
method:
[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 personPrototype = {
getFullName: function() {
return this.firstName + ' ' + this.lastName;
}
};
var john = Object.create(personPrototype, {
firstName: { value: 'John' },
lastName: { value: 'Doe' },
age: { value: 30 }
});
console.log(john.getFullName()); // 'John Doe'
[/dm_code_snippet]
The first argument to the Object.create()
method is the prototype, and the second argument is an object containing the properties and values of the object to be created. The Object.create()
method is an efficient way to create objects from prototypes, as the prototype object is not modified when creating the new object.
Conclusion
In this tutorial, we looked at how to create objects in JavaScript. We looked at object literals, constructor functions, and the Object.create()
method. Each of these methods has its own advantages and disadvantages, and you should use the one that best fits your needs.