'Whether to clone an object for a prototype

What is the difference between the following two ways to define a prototype, and is one more correct than the other?

// prototype
const animal = {
    breathe() {console.log('The animal is breathing.')},
}
// construtor
function Dog(name) {
    this.name = name;
}

Dog.prototype = animal; // without Object.create()

let a = new Dog('Annie');
a.breathe();

// prototype
const animal = {
    breathe() {console.log('The animal is breathing.')},
}
// construtor
function Dog(name) {
    this.name = name;
}

Dog.prototype = Object.create(animal); // with Object.create

let a = new Dog('Annie');
a.breathe();

Why would one be more preferable over the other (especially if it is a const object)?



Solution 1:[1]

Using Object.create adds an extra level to your prototype chain. The dog prototype will be an object with its prototype set to animal.

dog -> empty_object -> animal -> Object -> null

The first way is missing that extra object.

dog -> animal -> Object -> null

So if you want to add more functionality to just the dog, without adding to the animal, you need the former. Otherwise not needed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1