Description
In my application I have a series of model classes and they all extend the same base class. The BaseClass has a constructor implemented that when given a key value map will set the values of that map on the class.
Something like:
base-class.ts
class BaseClass {
constructor(json) {
for (var prop in json) {
this[prop] = json[prop];
}
}
}
Where my problem comes in is when extending the base class, If I define any default values for properties in the child class they will always override the values set by the constructor since I'm letting the parent class set the values, and that will always run first, then the defaults after.
For example:
Given the model person.ts
class PersonClass extends BaseClass {
public firstName: string = 'Derek';
}
A constructed PersonClass will always have 'Derek' as the firstName value regardless of what is passed to the constructor.
i.e.
var person = new PersonClass({firstName: 'Jack'});
console.log(person.firstName); // logs Derek, not Jack
If not for the default property value being set the property would work fine. I have put together a CodePen to see a working example, http://codepen.io/dotDeeka/pen/bEErjJ?editors=001. If you view the compiled JavaScript you can see quickly that the child class calls the parents constructor before setting the default property values.
I know languages like Java require you call the parent class as the very first thing in a constructor so I'm assuming that is what you were going for here.
Any thoughts?