__proto__ vs prototype

let cat = { breed: 'munchkin'}
let myCat ={ name: 'Sisi'}

Object.setPrototype(myCat, cat);

myCat.name(); // Sisi
myCat.breed();// munchkin
myCat.__proto__ // {breeed: 'munchkin'}, o referinta pt acel obiect

cat.tailLength =15;
myCat.__proto__ // {breeed: 'munchkin', tailLength: 15}
myCat.tailLength //15

– delegarea obiectelor catre alte obiecte

-prototype e pt functii

function Dog(){}

Dog.prototype.breed= 'Bulldog';
let myDog= new Dog();

myDog.breed;// 'Bulldog'

myDog.__proto__ ; // {breed: 'Bulldog'}
myDog.prototype; //undefined
Dog.prototype; // {breed:'Bulldog'} , a fost setat mai sus

function Giraffe() {};
Giraffe.prototype;// {}

let koala= {}
koala.prototype; // undefined
koala.__proto__ ; // {}
koala.__proto__== Object.prototype; //true
Object.prototype.sleep ='zzzzzz';
koala.sleep; //zzzzzz

__proto__ este proprietatea care pe un obiect, pointeaza catre prototipul care a fost setat

prototype este proprietatea pe o functie, care este setat ca proprietatea daca se foloseste  new

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s