Skip to content

Instantly share code, notes, and snippets.

@casperin
Last active August 29, 2015 14:26
Show Gist options
  • Save casperin/982b7e4f86adbf3e4eed to your computer and use it in GitHub Desktop.
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
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