this.constructor.prototype — can’t wholly overwrite, but can write individual props?

Why can’t I overwrite a constructor’s prototype from within the constructor?

You can, but it’s too late. The new instance has already been generated, inheriting from the old prototype. Maybe read how new works.

I don’t like how prototypes are usually defined externally from a constructor.

That’s just the way it is. You really should not setup the prototype from within the constructor – it would be executed everytime a new instance is created. That’s specifically what prototypes are not supposed to be. See also Assigning prototype methods *inside* the constructor function – why not?

and want to logically encapsulate things better.

You might want to have a look at the various (revealing) module patterns. Or maybe even at some Class framework.

I’m currently looking for more concrete reasons that I should not go forth with the pattern I’ve been presenting.

It does not work in Internet Explorer. It would not work in any ES5-compliant environment that does not support the __proto__ property. You should never use it set a prototype on an existing object. Instead, use Object.create (or its shim) for Correct javascript inheritance – which requires that you overwrite the prototype outside of the constructor.

My suggestion is to call your extend helper outside the constructor on it, which still has a nice syntax.

Leave a Comment