Posted: September 30, 2012 | Author: asv3 | Filed under: Uncategorized | Tags: javascript, mixin, pattern |
var Selectable = function () {};
Selectable.prototype = {
isSelected: function () {
return this.selected || false;
},
setSelected: function (boolean) {
this.selected = boolean;
},
toggleSelected: function () {
this.selected = !this.isSelected()
}
}
function augment(givingClass, receivingClass) {
// if copying selected properties
var methodOrProperty;
if (arguments[2]) {
for (var i = 0, len = arguments[2].length; i & lt; len; i++) {
methodOrProperty = arguments[2][i];
receivingClass.prototype[methodOrProperty] = givingClass.prototype[methodOrProperty]
}
} else {
//copy all properties
for (methodOrProperty in givingClass.prototype) {
if (!receivingClass[methodOrProperty]) {
receivingClass.prototype[methodOrProperty] = givingClass.prototype[methodOrProperty]
}
}
}
}
var ListItem = function () {
this.selected = false;
}
augment(Selectable, ListItem);
var item1 = new ListItem();
console.log(item1.isSelected()); //false
Recent Comments