0

I have this:

const util = require('util');

function Parent() {}
Parent.prototype.container = {}

function Child() {
  Parent.call(this);
}
util.inherits(Child, Parent);

Child.prototype.container['field'] = function(){}

Parent's prototype.container shouldn't have a .field property, right?

Interestingly, if you add another child, like so:

function AnotherChild() {
  Parent.call(this);
}
util.inherits(AnotherChild, Parent);

... it will also have the .field property, from the first Child.

wat.

sent1nel
  • 1,580
  • 1
  • 15
  • 31
  • Of course they would. They're all the same `.container` object. Don't use the prototype for things you don't want to share. – Bergi Apr 26 '18 at 08:16
  • Thanks https://stackoverflow.blog/2018/04/26/stack-overflow-isnt-very-welcoming-its-time-for-that-to-change/ – sent1nel Apr 26 '18 at 21:31
  • Sorry, have I offended you by closing the question? – Bergi Apr 26 '18 at 21:36
  • No, but `of course` when clearly I had a question about the bahavior of prototypes, provided code, and wrote in clear, concise English certainly didn't come across as "welcoming". – sent1nel Apr 26 '18 at 21:40
  • FYI, I ended up using Crockford's functional constructor pattern. – sent1nel Apr 27 '18 at 20:34

0 Answers0