$.extend Mixin
by southerd
HTML
<ul id="log"></ul>
JavaScript
var Starship = function(reg){
this.registry = "NCC-" + reg;
};
Starship.prototype.spedometer = function() {
$("#log").append($("<li />").text(this.registry + ' at ' + this.warpFactor_ || 0));
};
var WarpCore = {
warpFactor_: 0,
warpFactor: function(num) {
if(num === undefined) { //Getter
return this.warpFactor_;
} else {
this.warpFactor_ = num;
return this;
}
},
cruise: function(){
return this.warpFactor(5);
}
};
var enterprise = $.extend(new Starship('1701-D'), WarpCore);
enterprise.spedometer();
enterprise.warpFactor(8)
enterprise.spedometer();
enterprise.cruise()
enterprise.spedometer();