javascript - How can I create an object with a given prototype chain? -
i need way create empty object prototype chain provided array of objects:
var prototypes = [{one: 1}, {two: 2}, {three: 3}]; var created = create(prototypes); // prototype chain of created should be: // created --> {one: 1} --> {two: 2} --> {three: 3}
how can this?
you can't (currently*), because there one prototype chain, prototypes assigned @ object creation, , yet have multiple discrete objects there unrelated. is, create chain, need each object in chain backed previous one, aren't.
what can create chain copies of objects, of course subsequent changes original objects wouldn't reflected in chain.
you can close ecmascript5 creating new objects getters , setters of existing properties on source objects , syncing them, wouldn't handle new properties (or deleted ones), changes existing properties.
* said "currently" above because current draft specification next ecmascript version, ecmascript 6th edition, has new reflect
object has ability set prototype on existing object retrospectively. (whereas ecmascript5 , earlier, can set object's prototype when you're creating it.) of es6, you'd able assemble objects chain using future reflect.setprototypeof
method. right now, cross-browser, can't.
Comments
Post a Comment