0

If I try to add a method to prototype of an object, it doesn't work with this:

const dog = {
    color: 'black',
    bread: 'Spitz',
}

dog.prototype.hello = function() { // Doesn't work
    return `Hello, I'm a ${black} dog.`
}

console.log(dog);

But it works if I use __proto__ instead of prototype:

const dog = {
    color: 'black',
    bread: 'Spitz',
}

dog.__proto__.hello = function() { // Works
    return `Hello, I'm a ${black} dog.`
}

console.log(dog);

Why is that?

Axnyff
  • 9,213
  • 4
  • 33
  • 37
user1941537
  • 6,097
  • 14
  • 52
  • 99
  • 1
    The prototype for dog would be `Object.prototype` in this case. You could see what result editing that would have! – Berry M. Jul 23 '18 at 14:14
  • You don't need a prototype for an object literal. Simply put your `hello` method directly in the object. – Bergi Jul 23 '18 at 14:41

0 Answers0