JavaScript - Native Prototypes
JavaScript - Native Prototypes
Native Prototypes
Thenative prototypesin JavaScript are property ofObject.prototypeobject. The prototypes are the mechanism by which the objects inherit features from one another.
In JavaScript, each object contains theprototypeproperty. The prototype of each object contains themethodsandpropertiesrelated to the object. So, it is also called thenative prototype.
However, you can update or add new methods and properties to the native prototype object, but you canβt delete any already existing.
The JavaScript object and object constructor are the main prerequisites to understand JavaScript native prototypes.
Syntax
You can follow the syntax below to access the native prototype of the object.
Object.prototype;In the above syntax, the object can be any JavaScript object.
Example: Accessing the arrayβs prototype
Whenever you execute the below code in the browser, it will print the prototype of the array in the browser console. In the same way, you can check the prototype of the other objects.
In the console, you can see that the prototype object contains the methods which you can use with array methods.
Output
On running the above program, you will see the result in web console similar to the following screenshot β
Updating the Native Prototype
You can update the existing method or property of the native prototype object or add a new method or property to the native prototype object.
Syntax
You can follow the syntax below to update or add new properties or methods to the prototype object.
objName.prototype.name = valueIn the above syntax, objName is an object whose prototype you need to update.
The βnameβ is a method or property name. You can assign new values or function expressions to the βname.
Example: Updating the toLowerCase() method of the string objectβs prototype
The prototype of the String object contains the toLowerCase() method. Here, we update the toLowerCase() method.
The updated toLowerCase() method returns the string in the uppercase. In the output, you can observe the string, which is in uppercase.
In this way, you can update the functionality of the built-in methods of the object.
Output
After updating the string.toLowerCase() method: HELLO WORLDYou shouldnβt update the methods and properties of the native prototype object. However, you can add new as shown in the example below.
Example: Adding a new method to the prototype object
You can also add a new method to the Object prototype. Here, we added the firstCase() method in the object prototype.
The firstCase() method returns a string after converting the stringβs first character to the uppercase.
Output
After executing the string.firstCase() method: Hello worldAdding a method to the constructor function
Whenever you define an object using the constructor function, you canβt add a method or property to the constructor function using its instance. So, you need to add the method to the constructor function prototype. So it can be accessible through all instances of the object.
Example
In the example below, the Person() is a constructor function that initializes object properties.
After that, we added the display() method to the prototype of the person() function.
Next, we created two instances of the Person() function and used the display() method with them. So, methods and properties added in the prototype of the object constructor can be accessed through all instances of the constructor function.
Output
1, James
2, NayanAll instances of the object inherit the properties and methods from their parentβs prototype.
JavaScript Prototype Chaining
Simply, you can say that the prototype stores the default values of the properties. The code overrides the prototype property value if the object constructor and its prototype contain the same properties.
Example
In the below code, the Person() function contains the name property. We have added the name and age property in the functionβs prototype.
We have created the p1 object using the Person() constructor function. The value of the name property of the p1 object is βNayanβ as the name already exists in the constructor. The value of the age property is 20, which is the same as the age property value in the prototype.
Output
Id: 1, Name: Adam, Age: 20