Object.create()

  • 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

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s