New keyword vs Object.create() method

If I choose to use the Object.create() method to create an instance instead of using the new keyword, does it mean that the instance won’t get a copy of the own properties in the constructor function?


function Animal() {
  this.numLegs = 2
 }

Animal.prototype = {
  constructor: Animal,
  eat: function() {
    console.log("nom nom nom");
  }
};

let duck = Object.create(Animal.prototype)
let beagle = new Animal

console.log(duck.numLegs)      // undefined
console.log(beagle.numLegs)   // 2
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36 Edg/94.0.992.31

Challenge: Inherit Behaviors from a Supertype

Link to the challenge:

This the same as new Animal() (you get the exact same type of object created), except that it isn’t going to execute the constructor. There is no call to do that, and the only way you can assign numLegs to your new object at initialisation time is if you execute the constructor function

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.