Skip to content

Instantly share code, notes, and snippets.

@ramonvictor
Last active December 10, 2016 18:38
Show Gist options
  • Save ramonvictor/b04c4db0a59bde389159f45069068db2 to your computer and use it in GitHub Desktop.
Save ramonvictor/b04c4db0a59bde389159f45069068db2 to your computer and use it in GitHub Desktop.
`this.super()` concept using `arguments.callee.caller`
(function() {
// 'use strict';
// ^ would throw error because of `arguments.callee.caller`.
var ModuleCore = function() {};
ModuleCore.prototype.html = function() {
console.log(':: html');
};
ModuleCore.extend = function (settings, Parent) {
var parent;
if (typeof Parent !== 'undefined') {
parent = new Parent();
} else {
parent = new ModuleCore();
}
var M = function() {};
M.prototype = Object.assign(parent, settings);
M.extend = function(settings) {
settings.super = settings.super || function() {
var caller = arguments.callee.caller;
parent[caller.name].apply(this, caller.arguments);
};
return ModuleCore.extend.call(null, settings, M);
};
return M;
};
var m = ModuleCore.extend({
render: function() {
this.html();
console.log('m1 render!', arguments);
}
});
var m2 = m.extend({
render: function() {
// `this.super()` will automaticaly 'guess'
// function name ('render') as well as `arguments` value.
this.super();
console.log('m2 render!');
}
});
var foo = new m2();
console.log(foo.render(1));
// :: html
// m1 render! [1]
// m2 render!
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment