Fiddling prototypal inheritance
Trying to find a way round to architecture application using proper prototypal inheritance. Use case: how to mix different behavior into an object. Thinking about factories, prototypes, etc.
by espeon
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.3.1/lodash.min.js"></script>
JavaScript
// factories/car.js
var carPrototype = {
drive: function () {
console.log("They see me rollin'");
},
bip: function () {
console.log("no bip here");
}
};
function createCar() {
var car = Object.create(carPrototype);
car.tires = 4;
return car;
}
// prototypes/wheel.js
var wheel = {
turn: function () {
console.log("let's turn");
}
};
// prototypes/warning.js
var warning = {
bip: function () {
console.log("biiiiip biiiiip mozerfocker! ");
}
};
// main.js
var car = createCar();
var warningAndWheel = _.merge({}, wheel, warning);
var carWithWarning = _.merge(car, warningAndWheel);
carWithWarning.bip();