Saturday, October 16, 2021

Change the Prototype to a New Object


 function Dog(name) {

  this.name = name;
}

Dog.prototype = {
  // Only change code below this line
  numLegs : 4,
  eat: function(){
    console.log("Food Food Food")
  },
  describe: function(){
    console.log("My name is "+this.name);
  }

};

let beagle = new Dog("Tiger");

for(let x in beagle){
  if(beagle.hasOwnProperty(x)){
    console.log('ownProps',x);
  }else{
    console.log('prototypes:',x)
  }
}

//result
 /*
 ownProps name
prototypes: numLegs
prototypes: eat
prototypes: describe
 */

No comments:

Post a Comment

JavaScript Functions as JavaScript Variables

In Javascript instead of declaring and executing a function in two different steps. for example step 1 -  function add(a,b){return a+b;} ste...