Prototype Objects

This lesson explains the prototype objects, what they are, how they are accessed and why they are used to add properties.

In the previous lesson, we asked the question regarding whether there was a simpler approach for adding new methods/properties to a constructor function. This introduces us to the very major concept of “Prototype Objects” in Javascript.

Before we get into how it can be used, let’s discuss what a prototype is.

Prototype Properties in Objects

Instead of jumping directly into the definition, let’s begin by looking at the code below:

Press + to interact
//creating an constructor function named EmployeeConstructor
function EmployeeConstructor(_name,_age,_designation) {
this.name = _name,
this.age = _age,
this.designation = _designation
}
//creating an object from EmployeeConstructor using "new" keyword
//The object created is stored in the variable employeeObj
var employeeObj = new EmployeeConstructor('Joe',22,'Developer')
console.log(employeeObj)

As seen above, if we display employeeObj the three properties: name, age and designation are displayed, encapsulated inside the EmployeeConstructor object created when the new command is used, along with their values.

If the same code is run locally on the browser, the browser console will display the ...

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy