Here's an example code snippet where I am trying to understand Object.create:
function Animal {}
var cat = new Animal();
I have a requirement to create 100 animals and add a method called saySomething() to the constructor. To achieve this, I add the method to the prototype so that it is created only once and every instance can access it:
Animal.prototype.saySomething = function(voice) {
console.log(voice)
}
Now, let's consider creating objects in a different way:
var Animal = {
saySomething: function(voice) {
console.log(voice)
}
}
var cat = Object.create(Animal);
var dog = Object.create(Animal);
...
The question arises - when I create objects like this, is the saySomething method created for each instance separately? How can I add this method to the Animal object in the same manner as before? It's a bit confusing for me at the moment. Please help!