Last active
August 29, 2015 14:26
-
-
Save casperin/982b7e4f86adbf3e4eed to your computer and use it in GitHub Desktop.
Adding a `this._super()` that passes arguments along to a function's scope
This file contains 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 addSuper (subFn, superFn) { | |
return function () { | |
var self = this, | |
args = arguments; | |
this._super = function () { | |
return superFn.apply(self, args); | |
}; | |
return subFn.apply(self, args); | |
} | |
} | |
// Example usage: | |
function supr () { | |
console.log('supr was called with', arguments); | |
} | |
function inc (x) { | |
this._super(); | |
return x + 1; | |
} | |
inc = addSuper(inc, supr); | |
inc(2); | |
// console: 'supr was called with [2]' | |
// → 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment