sylvainpolletvillard / ObjectModel

Strong Dynamically Typed Object Modeling for JavaScript
http://objectmodel.js.org
MIT License
467 stars 30 forks source link

Properties not passed when calling super() in derived class constructor #117

Closed andriichern closed 4 years ago

andriichern commented 4 years ago

I have two objects

class Base extends ObjectModel({ id: String, type: String, required: Boolean }) { constructor(obj, type) { super({ // property initialization }); } }

and

class Derived extends Base.extend({ values: Array }) { constructor(obj) { super({ ...obj, values: // property initialization code }, 'typeStr'); } }

The problem is when I call super() inside Derived class constructor second parameter in Base constructor is always undefined. Tried different call variants - got no real value, always undefined

sylvainpolletvillard commented 4 years ago

Sorry for the delay.

I rewrote your code with more detailed steps:

const BaseModel =  ObjectModel({ id: String, type: String, required: Boolean })

class Base extends BaseModel { 
   constructor(obj, type) {
     super({ ...obj, type }); 
   }
}

const DerivedModel = Base.extend({ values: Array })

class Derived extends DerivedModel { 
   constructor(obj) { 
       super({ ...obj, values: [] }, 'typeStr');
   }
}

With this code, the Derived class extends an ObjectModel (DerivedModel). So when calling super, you invoke the DerivedModel constructor, not the Base constructor. DerivedModel is an ObjectModel so its constructor takes only one argument: an object. That's why second argument is always undefined.

image

The solution is to rewrite constructors so that they all match the same signature, that is, a single object as argument. Personnally, I consider consistent constructor signatures to be a good practice.