When you use the ‘new’ keyword to create an instance of a constructor function, it seems to be able to access both methods within the constructor function as well as methods from the prototype. However, when using the Object.create() function it doesn’t seem to be able to call methods that are contained within the constructor function - only methods that are contained within the prototype. Why is this?
For example:
function Animal() {
this.age = 10;
this.bark = function () {
console.log("Woof Woof")
}
}
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
let dog0 = new Animal();
let dog1 = Object.create(Animal);
dog0.bark() // "Woof Woof"
dog0.eat() // "nom nom nom"
dog1.bark() // "Uncaught TypeError: dog1.bark is not a function"
dog1.prototype.eat() // "nom nom nom"
Why do I seem to not be able to access the bark method of the constructor function when creating dog1 using Object.create() instead of ‘new’?