prototype

 // objects inherit properties and methods 

from a prototype:

//prototype property allows you
 to add new properties to object constructors:
//class..
1:---
function obj(info){
    this.name=info;

}
obj.prototype.address="gurugram";
obj.prototype.showdata=function(){
    return this.address;
}

let obj1=new obj('raj')
console.log(obj1.showdata());

2:-----
class A{
    name;
    age;
    constructor(n,a){
        this.name=n;
        this.age=a;
    }
}
ob=new A("amit",23);
console.log(ob.name);
ob1=new A("raj",34);

ob.address="gurugram";
//console.log(ob);
//console.log(ob1.address);
//classname.prototype.key='value';
A.prototype.address="gurgram";
console.log(ob1);
console.log(ob1.address);

..........................
//let a=[1,2,3,4];
//additional property..by prototype..
//array object functon:---all object..
let user={
    "name":"gaurav",
    "rollno":2
}

let user1={
    "name":"gaurav1",
//    "rollno":3
}
user1.__proto__=user;//prototype inheritance..
console.log(user1);

...............
The Object.prototype is on the top of the prototype inheritance chain:
console.log(Object.prototype);
console.log(user1);








Comments

Popular posts from this blog

interview questions js[ Anurag Singh ProCodrr]

reactnative_creation