0

When not using the 'new' keyword, the constructor logged in the console shows what the constructor of the object is and it also changes when I change the __proto__ of the object it the console also displays what the current constructor of the object is. But when using the 'new' keyword even I change the __proto__ the console still shows the constructor of the original constructor that made the object.

Without 'new' keyword photo Without 'new' keyword photo

With 'new' keyword photo

With 'new' keyword photo

The console displays what is the current constructor of the object is, and it changes when I change its __proto__

function Human(){}
function Animal(){}
const obj = Object.create(Human.prototype)
console.log(obj)
Object.setPrototypeOf(obj, Animal.prototype)
console.log(obj)

But when using the new keyword even if change the __proto__ of the object it still displays the original constructor it was created from.

function Human(){}
function Animal(){}
const obj = new Human()
console.log(obj)
Object.setPrototypeOf(obj, Animal.prototype)
console.log(obj)
saketh
  • 803
  • 1
  • 10
  • 24
Chin Zhao
  • 61
  • 6
  • I think this will answer your question: https://stackoverflow.com/questions/4166616/understanding-the-difference-between-object-create-and-new-somefunction – DigitalJedi May 21 '19 at 16:13
  • Possible duplicate of [Understanding the difference between Object.create() and new SomeFunction()](https://stackoverflow.com/questions/4166616/understanding-the-difference-between-object-create-and-new-somefunction) – DigitalJedi May 21 '19 at 16:14
  • can you post Human and Animal class definitions? – GJCode May 21 '19 at 16:17
  • you may also want to take a look at this article on how prototypes and inheritance work in javascript: https://developer.mozilla.org/it/docs/Web/JavaScript/Inheritance_and_the_prototype_chain – GJCode May 21 '19 at 16:20

0 Answers0