Skip to main content

Command Palette

Search for a command to run...

prototype in Javascript

Published
1 min read
prototype in Javascript

prototype in Javascript:

example: let object1= { name: "aditya", city:"blr", age: "23" };

let object2 = { name: "sameer", gender: "male" };

object1.proto= object2;

// inherited the property the object2 into the object1, we can access the property of the object2 from object1.

object1.name // "aditya"

object1.gender // "male"

object2.gender // "male"

example 2:

var Mobile= function(mname, mcolor, mprice){
     this.name= mname;
     this.color= mcolor;
     this.price= mprice;
}

var samsung= new Mobile("nokia", "red", 4000);

samsung //{name: "nokia", color: "red", price: 4000}

Mobile.prototype.fullDetails= function(){

    return this.name + this.color + this.price;
}

var samsung= new Mobile("galaxy", "red", 4000);

var nokia= new Mobile("lumia", "blue", 3000);

fullDetails method can be accessed for all object created from mobile constructor.

samsung.fullDetails(); // "nokiared4000"