What gets logged?
function Dog(name) { this.name = name this.speak = () => 'Woof Woof' } Dog.prototype.speak = function() { return 'Ruff Ruff' } const dog = new Dog('Leo') console.log(dog.speak())
What gets logged?
function Dog(name) { this.name = name this.speak = () => 'Woof Woof' } Dog.prototype.speak = function() { return 'Ruff Ruff' } const dog = new Dog('Leo')
Woof Woof gets logged.
Before JavaScript delegates the lookup of the property to the Constructor’s prototype, it first checks to see if the property exists on the object being returned from the Constructor. In this case, it does so it calls it.
For more info., check out A Beginner’s Guide to JavaScript’s Prototype