in "JavaScript you don"t know", there is a paragraph about the inherited mixin implementation. The code is as follows:
function mixin( sourceObj, targetObj ) {//sourceObjtargetObjkey,targetObj
for (var key in sourceObj) {
//
if (!(key in targetObj)) {
targetObj[key] = sourceObj[key];
}
}
return targetObj;
}
var Vehicle = {
engines: 1,
ignition: function() {
console.log( "Turning on my engine." );
},
drive: function() {
this.ignition();
console.log( "Steering and moving forward!" );
}
};
var Car = mixin( Vehicle, {
wheels: 4,
drive: function() {
Vehicle.drive.call( this );
console.log(
"Rolling on all " + this.wheels + " wheels!"
);
}
} );
Car.drive();
the output is as follows:
Turning on my engine.
Steering and moving forward!
Rolling on all 4 wheels!
just called the drive () method. Why is the content in ignition () printed? Where is it used wrong?