You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Instantiate Object with Object.create (OLD METHOD)
// METHOD #1functionAnimal(name,energy){letanimal=Object.create(Animal.prototype);animal.name=name;animal.energy=energy;returnanimal};Animal.prototype.play=function(length){console.log(`${this.name} is playing ${length} hours`);};leo=Animal('Leo',7);console.log('leo name:'+leo.name);console.log(leo.play(5));// METHOD #2constAnimal={play: function(length){console.log(`${this.name} is playing ${length} hours`);},eat: function(length){console.log(`${this.name} is eating ${length} hours`);}};varleo=Object.create(Animal);leo.name='Leo';/*// alternative this works tooconst leo = Object.create(Animal, { name: {value: 'Leo'}});*/leo.play(2);
Instantiate Object with new keyword (NEW METHOD)
functionAnimal(name,energy){this.name=name;this.energy=energy;};Animal.prototype.play=function(length){console.log(`${this.name} is playing ${length} hours`);};constleo=newAnimal('Leo',7);console.log('leo name:'+leo.name);console.log(leo.play(5));
change/add constructor value
functionAnimal(name,energy,minute){this.name=name;this.energy=energy;this.sleep=minute;};Animal.prototype.changeSleep=function(minute){this.sleep=minute;};Animal.prototype.addEat=function(seconds){this.eat=seconds;};varleo=newAnimal('Leo',7,2);console.log(`${leo.name} is sleeping ${leo.sleep} minutes and is eating ${leo.eat} seconds.`);leo.changeSleep(3);leo.addEat(4);console.log(`${leo.name} is sleeping ${leo.sleep} minutes and is eating ${leo.eat} seconds.`);
Call another constructor
functionAnimal(name,energy){this.name=name;this.energy=energy;};functionZoo(name,energy,sleep){Animal.call(this,name,energy);this.sleep=sleep;};varZooGermany=newZoo('Leo',1,2);console.log(`${ZooGermany.name} is sleeping ${ZooGermany.sleep} minutes and has ${ZooGermany.energy} energy.`);
Inherit Prototype (How to access other constructor prototypes)
functionAnimal(name,energy){this.name=name;this.energy=energy;};Animal.prototype.eat=function(seconds){this.eat=seconds;console.log(`${this.name} is eating for ${seconds} seconds`);};functionZoo(name,energy,country){Animal.call(this,name,energy);this.country=country;};varPeter=newAnimal('Peter',1);Zoo.prototype=Object.create(Animal.prototype);// get prototypes from Animal constructorvarZooGermany=newZoo('Peter',1,'Germany');Peter.eat(1);// <-- will workZooGermany.eat(1);// will result in error if we delete the Object.create line cause eat prototype not avaible
Change constructor
functionAnimal(name,energy){this.name=name;this.energy=energy;};functionZoo(name,energy,country){Animal.call(this,name,energy);this.country=country;};varZooGermany=newZoo('Peter',1,'Germany');Zoo.prototype.constructor=Zoo;// <-- for default constructor Zoo will use the Animal constructor.console.log(ZooGermany);
for(constkeyinleo){console.log(`Key: ${key} - Value: ${leo[key]}`);}/*Key: name - Value: LeoKey: energy - Value: 7Key: play - Value: function (length) { console.log(`${this.name} is playing ${length} hours`);}*/
Iterate only over own properties and ignore prototypes
for(constkeyinleo){if(leo.hasOwnProperty(key)){console.log(`OWN - Key: ${key} - Value: ${leo[key]}`);}elseconsole.log(`NOT OWN - Key: ${key} - Value: ${leo[key]}`);}/*OWN - Key: name - Value: LeoOWN - Key: energy - Value: 7NOT OWN - Key: play - Value: function (length) { console.log(`${this.name} is playing ${length} hours`);}*/
instanceof
Check if object is an instance of a specific class