Last active
December 10, 2016 18:38
-
-
Save ramonvictor/b04c4db0a59bde389159f45069068db2 to your computer and use it in GitHub Desktop.
`this.super()` concept using `arguments.callee.caller`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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