Saturday, October 16, 2021

Iterate Over All Properties

 function Dog(name) {

  this.name = name;
}

Dog.prototype.numLegs = 4;

let beagle = new Dog("Snoopy");

let ownProps = [];
let prototypeProps = [];

// Only change code below this line

for(let x in beagle){
  if(beagle.hasOwnProperty(x)){
    ownProps.push(x)
  }else{
    prototypeProps.push(x)
  }
}

console.log(ownProps,"and",prototypeProps)
//result
//[ 'name' ] and [ 'numLegs' ]

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...