I am learning composition in Javascript and was wondering why Object.assign() in my code is adding methods to the prototype's constructor rather than the object's constructor. I will link the code below. Basically, the code is adding two methods (eat and walk) to the Person object by using Object.assign(). But, as the image below shows, the console is displaying the methods as belonging to the Person.prototype's constructor rather than the Person object itself. I must be overthinking it. Any help is appreciated. Thanks!
Code
const canEat = {
eat() {
this.hunger--;
console.log('eating');
}
};
const canWalk = {
walk: function() {
console.log('walking');
}
};
const canSwim = {
swim: function() {
console.log("swim");
}
};
function Person() {
}
Object.assign(Person,canEat,canWalk);
const person = new Person();
console.log(person);


When
Object.assignis evaluated:eatandwalkproperties are assigned toPersonfunction.When the instance of
Personis created:Then we have a new object which has a constructor property pointed to the
Personfunction. However the new object has neithereatnorwalkproperties (those belong toPerson[constructor] function).If you want to make
eatandwalkproperties available for instances ofPerson, you should assign those methods toPerson.prototypeinstead:Because when an object is created with new operator, it inherits not from the constructor function
Person, but from constructor function prototypePerson.prototype.