Super Class
overwrite a method when mixin class, but mantain access to the original as a $super method
by gschutz
JavaScript
function Class() {
var data = [];
this.add = function(a) {
data.push(a);
return this;
}
this.remove = function(a) {
data.splice(data.indexOf(a), 1);
return this;
}
this.data = function() {
return data;
}
}
function OtherClass() {
this.add = function(a) {
console.log('do something else add');
this.$super(a);
return this;
}
this.remove = function(a) {
console.log('do something else remove');
this.$super(a);
return this;
}
this.data = function() {
console.log('do something else data');
return this.$super();
}
}
function SuperClass(A, B) {
var cl = new A();
var su = new B();
for (var k in cl) {
if (cl.hasOwnProperty(k)) {
if (k instanceof function) {
this.$super = cl[k];
(function() {
this[k] = function() {
return this;
};
}.call(this));
} else {
this[k] = cl[k];
}
}
}
}
var user = new SuperClass();
user.add('opa').add('oma').remove('opa');
console.log(user.data());