I'm trying to use inheritance, but I'm stuck. I'm getting an error and don't know what I'm doing wrong. All examples I see online don't pass objects into their constructors, so I'm not sure what I should be doing. Here is a dumbed down example -
function Automobile(obj){
this.name = obj.name;
this.model = obj.model;
}
Automobile.prototype = {
getName: function(){
console.log(this.name);
},
getModel: function(){
console.log(this.model);
}
}
var bmw = new Automobile({
name: 'BMW',
model: 'm5'
})
bmw.getName();
bmw.getModel();
function Truck(obj){
this.cabSize = obj.cabSize
}
Truck.prototype = new Automobile();
Truck.prototype = {
getCabSize: function(){
console.log(this.cabSize);
}
}
var fordF150 = new Truck({
name: 'Ford',
model: 'F150'
})
//Uncaught TypeError: Cannot read property 'name' of undefined test.js:2
//Automobile test.js:2
//(anonymous function)
Truck.prototype = new Automobile();its expecting the first parameter to be an object.