ecmascript 6 - Is ES6 class extend fully equivalent to Object.assign based extending of an object? -
in other words these 2 different blocks of code equivalent?
es6 class extend based
class child extends parent { // define subclass } var myinstance = new child();
object.assign based
var myinstance = object.assign(new parent(), { // define subclass }
in particular use case trying extend (facebook's) flux dispatcher. in examples use object.assign. es6 class extend, worried there subtle differences between 2 should stick object.assign.
no, these code blocks not equivalent
in class inheritance example new constructor makes objects having features parent class.
extending objects via object.assign
example of mixins. add properties 1 instance not change future children.
unlike child classes, instance after extension still have constructor
property pointing parent
. means can't recognize extended child among non-extended, because have same constructor , operator instanceof
give same result. can't access overridden methods in child, because lose link it.
as flux's dispatcher in example, can't extend via class inheritance, because there no constructor can provide parent.