- Object.create() este o metoda statica care creaza un nou obiect cu prototype-ul setat la un obiect anume
const cat={
makeSound: function() {
console.log('miau');
}
}
cat.makeSound(); //miau
const cat={
makeSound: function() {
console.log(this.sound);
}
}
const mark= Object.create(cat);// creaza un nou obiect si va seta prototipul
acelui obiect sa fie cat
mark.sound= 'miau';
mark.makeSound(); //miau
console.log('Is mark a cat?', cat.isPrototypeOf(mark)); //true
-Object.create() este de preferat pt prototype model in locul new
const cat={
init: function(sound){
this.sound= sound
}
makeSound: function() {
console.log(this.sound);
}
}
function objectCreate(proto) {
const obj ={}
Object.setprototypeOf(obj, proto);
return obj;
}
const mark= objectCreate(cat);// creaza un nou obiect si va seta prototipul
acelui obiect sa fie cat
mark.init= 'miau';
mark.makeSound(); //miau
console.log('Is mark a cat?', cat.isPrototypeOf(mark)); //true
Concluzii:
-Object.create() este o metoda statica care creaza un nou obiect cu prototype-ul setat la un obiect anume