Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I should point out that:

  // Set Bar's prototype to a new instance of Foo
  Bar.prototype = new Foo();
  Bar.prototype.foo = 'Hello World';
Is an antipattern. Use

  Object.create()
to set up your prototype chain, i.e.:

  Bar.prototype = Object.create(Foo.prototype);
  Bar.prototype.foo = 'Hello World';
I understand this information is dated, but it should be updated as the language evolves and standards change. Surprises me to still see this floating around. You do not want to invoke your constructor when you set a prototype. If you want to use the parent constructor's behaviour in the child, it should be done in your child constructor using Foo.call(this, ...) or Foo.apply(this, [...]).

Object.create is supported by all modern browsers, and the Object.create polyfill can be found in Mozilla's documentation:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: