I have created two objects:
- Mammal
- Cat
Cat extends Mammal. Both objects have constructor which takes one parameter called config. I am trying to overwrite Mammals constructor in Cat's constructor but I am getting strange results:
function Mammal(config) {
this.config = config;
console.log(this.config);
}
function Cat(config) {
// call parent constructor
Mammal.call(this, config);
}
Cat.prototype = new Mammal();
var felix = new Cat({
"name": "Felix"
});
This prints in the console:
undefined fiddle.jshell.net/_display/:23
Object {name: "Felix"}
Why is the parent constructor called twice? And why, when it's called the first time, this.config is undefined? I am assigning the property. Could you help me fix this code?