Examples are taken from You Don't Know JS this & Object Prototypes (here is the specific chapter, scroll down a bit or just search for the word "mixin", "explicit" or "implicit", the articles should show up).
In that book, a "mixin" is explained as "mixing in the behavior of different objects" (paraphrasing).
However, I can't comprehend the difference between Explicit and Implicit mixins.
Here is an explicit mixin:
// vastly simplified `mixin(..)` example:
function mixin( sourceObj, targetObj ) {
for (var key in sourceObj) {
// only copy if not already present
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!" );
}
} );
And here is an implicit mix-in:
var Something = {
cool: function() {
this.greeting = "Hello World";
this.count = this.count ? this.count + 1 : 1;
}
};
Something.cool();
Something.greeting; // "Hello World"
Something.count; // 1
var Another = {
cool: function() {
// implicit mixin of `Something` to `Another`
Something.cool.call( this );
}
};
Another.cool();
Another.greeting; // "Hello World"
Another.count; // 1 (not shared state with `Something`)
The only difference I can find is that the first example uses a function to copy all the behavior of Vehicle into Car, while the second example only borrows a single method. So I think in the first example, Vehicle's behavior is explicitly copied into Car, while in the second example, only a method is referenced with its this bound to Another, therefore an implicit mix-in ? Is this where the difference lies ? if that is the difference, then the first example contains an example of an Implicit Mix-in as well, since it also references and binds the this of one of Vehicle's methods. If my assumptions are correct, then IMO, the first example should be called a "Full mix-in" and the latter should be called "Partial mix-in", since these names fit the behaviors better.
I searched online and aside from the You Don't Know JS chapter, only 1 article showed up, and that one didn't mention "implicit mix-in" or "explicit mix-in".
So, what is the difference ?