Saturday, October 25, 2025
HomeLanguagesJavascriptJavaScript Object Prototypes

JavaScript Object Prototypes

JavaScript prototypes are used to access the properties and methods of objects. Inherited properties are originally defined in the prototype or parent object. The Date object is inherited from Date.prototype, Array object inherits from Array.prototype, etc. The prototypes may be used to add new properties and methods to the existing objects and object constructor.

Syntax:

Object.prototype

Example 1: This example adds a new property to the object.

Javascript




function Student(a, b) {
    this.name = a;
    this.id = b;
}
 
Student.prototype.age = 12;
 
const s1 = new Student("Dinesh", 1234567);
 
console.log(s1.name +
    " is " + s1.age + " years old.");


Output

Dinesh is 12 years old.

Example 2: This example adds a new method to the object.

Javascript




function Student(a, b) {
    this.name = a;
    this.id = b;
}
 
Student.prototype.details = function () {
    return this.name + " " + this.id
};
 
let s1 = new Student("Dinesh", 1234567);
 
console.log(s1.details());


Output

Dinesh 1234567

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS