0

E.g., in this very primitive and simplified piece of code:

function Parent() { }
function Child() { }
Object.setPrototypeOf(Child.prototype, Parent.prototype); // OR: "Child.prototype.__proto__ = Parent.prototype;" (the result is totally identical)

versus:

function Parent() { }
function Child() { }
Child.prototype = Object.create(Parent.prototype);

Then I check these two pieces of code the following way:

console.log(child instanceof Child); // true for both examples
console.log(child instanceof Parent); // true for both examples
console.log(Parent.prototype.isPrototypeOf(child)); // true for both examples
console.log(Child.prototype.isPrototypeOf(child)); // true for both examples

However: checking against the next two lines leads to different results —

console.log(JSON.stringify(Object.getOwnPropertyNames(Child.prototype), null, 2)); // ["constructor"] vs []
console.log(child.__proto__); // a constructor property is shown (on the first place) only in the first case and no constructor property – in the second case

— whereby only in the first case Parent constructor is displayed (see the images attached). What causes this difference?

Roman Karagodin
  • 740
  • 2
  • 11
  • 16
  • Please show text output as text, not pictures of text. The first log could be output via `console.log(JSON.stringify(Object.getOwnPropertyNames(Child.prototype), null, 2))`. The second log by simply noting that `child.__proto__` has a `constructor` property on the first and a `__proto__` property on the second. – Heretic Monkey Nov 12 '20 at 14:09
  • 1
    You may also want to look at [Extends Object.setPrototypeOf() vs Object.create](https://stackoverflow.com/q/58377377/215552) – Heretic Monkey Nov 12 '20 at 14:11
  • See also [Defining inheritance chain in JS - why recreate prototype?](https://stackoverflow.com/q/63438513/1048572) – Bergi Nov 12 '20 at 14:33

0 Answers0