- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create an object with prototype in JavaScript?
In this article, we are going to explore prototypes and their creation in JavaScript. We will learn about how to create objects and how to implement them with the help of examples.
Prototypes can be defined as a mechanism that lets the objects of JavaScript inherit features and properties from another object. A prototype property is also an object whose methods and properties can be inherited by a new Object.
A real-life object always has some properties associated with it. For instance, a Student object should have the following properties: name, rollNumber, class, subjects, age, etc.
We can create an Object using prototypes in multiple ways. Some are defined below −
1. Object Literal
The first method of creating an object is by using the Object Literal. It describes the methods and properties of the object that will be inherited directly.
Syntax:
var Object = {name: "value", id: "number", category: "section"}
Example 1
In the below example, we are creating an object by using the object literal.
# index.html
<!DOCTYPE html> <html> <head> <title> Creating Objects from Prototype </title> </head> <body> <h2 style="color:green"> Welcome To Tutorials Point </h2> </body> <script> var employee = { name: "Steve", age: 34, designation: "CEO", empId: 21, manager: "Mark" } console.log(employee); </script> </html>
Output
On successful execution of the above program, the browser will display the following result −
Welcome To Tutorials Point
And in the console, you will find all the results, see the screenshot below −
2. Prototypes
Every JavaScript function has a default prototype object. This prototype can be used for creating an object by initializing methods and properties.
Syntax
let obj = Object.create(Object_prototype)
Example 2
In the below example, we are creating an Object from a Prototype.
# index.html
<!DOCTYPE html> <html> <head> <title> Creating Objects from Prototype </title> </head> <body> <h2 style="color:green"> Welcome To Tutorials Point </h2> </body> <script> let student = { id: 21, class: "XI", subjects: "PCM" } let studentV1 = Object.create(student); console.log("Id: " + studentV1.id); console.log("Class: " + studentV1.class); console.log("Subjects: " + studentV1.subjects); </script> </html>
Output
On successful execution of the above program, the browser will display the following result −
Welcome To Tutorials Point
And in the console, you will find all the results, see the screenshot below −
3. Constructor
We can use the constructor for defining an object and its properties. this keyword is used for assigning the values to these properties of an object.
Syntax:
Prototyping the Object
function Object(property1, property2) { this.property1 = property1; this.property2 = property2; } var obj = new Object( property1, property2 );
Defining the Object with the above constructor
let obj = new Object(property1, property2, property3)
Example 3
In the below example, we are going to use constructors for defining the object and its properties.
# index.html
<!DOCTYPE html> <html> <head> <title> Creating Objects from Prototype </title> </head> <body> <h2 style="color:green"> Welcome To Tutorials Point </h2> <script> function Student(name, rollNo, subjects) { this.name = name; this.rollNo = rollNo; this.subjects = subjects; } // Defining the prototype console.log("Student Prototype: " + Student.prototype); // Creating an Object from the prototype let student1 = new Student('Steve', 21, 'PCM'); console.log(student1); let student2 = new Student('Mark', 30, 'Commerce'); console.log(student2); </script> </body> </html>
Output
On successful execution of the above program, the browser will display the following result −
Welcome To Tutorials Point
And in the console, you will find all the results, see the screenshot below −
- Related Articles
- How to access a JavaScript object using its own prototype?
- How to create a JSON object in JavaScript? Explain with an example.
- How does JavaScript .prototype work?
- How to create an object and access its properties in JavaScript?
- How to create an Autocomplete with JavaScript?
- JavaScript: How to Create an Object from Key-Value Pairs
- How to create RegExp object in JavaScript?
- How to create object properties in JavaScript?
- How to create an object property from a variable value in JavaScript?
- Adding a function for swapping cases to the prototype object of strings - JavaScript
- How to create a Boolean object in JavaScript?
- How to create a custom object in JavaScript?
- How to create a Date object in JavaScript?
- How to create a multidimensional JavaScript object?
- JavaScript Array prototype Constructor
