-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Should we be using Object.create() to hook up prototypes? #872
Comments
Prototype chains while awesome are a bitch to work with in reality. The best idea is to steer clear of them altogether by doing something like duck punching:
Since most prototypes don't contain objects (excluding functions), this works extremely well to flatten the inheritance structure. Additionally, if you need to reference a 'super' function at some point, you grab it out of the parent proto and reference it explicitly.
|
That article has some misconceptions, e.g. there is really no need to duplicate properties in both object literal and initializer, you first override properties from prototype and then call initilizer - not the other way around. I would strongly recommend you to read this article as a better reference: http://killdream.github.com/blog/2011/10/understanding-javascript-oop/ If you are intending to use Object.create() then you could ditch constructors altogether. With Object.create and couple of helper functions it's much easier to build prototype chains than with constructors: https://gist.github.com/1558929 |
Everyone seems to have used this issue to bring up their personal favorite way to do inheritance in JavaScript. I'll avoid that, and say: yes, switching to |
Reviewed. |
Assigning to sprint 18. It turns out that using |
…get base class. This addresses #2221 and #2218. As part of this, cleaned up some issues around how we're doing inheritance: * For #872, switched to using `Object.create()` to hook up prototypes (this avoids subtle bugs when binding event handlers in base classes). * Made it so all child classes call base class implementation for overridden functions (even if the base class impl is empty right now). The one exception is `close()`, which is a bit weird right now--will file a separate issue on this.
@njx: do you want to do anything more here before closing? |
Nope, I think we're good. Closing. |
Currently, our prototype inheritance for inline editors is set up like this (with details omitted):
I think this means that the
InlineTextEditor()
constructor gets called twice--once for the prototype itself, and then once each time we construct the editor. Not a huge deal, but it seems redundant. Should we be doingMultiRangeInlineEditor.prototype = Object.create(InlineTextEditor.prototype)
instead?See the last section in http://www.bennadel.com/blog/2184-Object-create-Improves-Constructor-Based-Inheritance-In-Javascript-It-Doesn-t-Replace-It.htm
The text was updated successfully, but these errors were encountered: