Skip to content

Instantly share code, notes, and snippets.

@polarblau
Created July 10, 2014 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save polarblau/4e2e0acef7a325c261aa to your computer and use it in GitHub Desktop.
Save polarblau/4e2e0acef7a325c261aa to your computer and use it in GitHub Desktop.
function Bar() {}
Bar.prototype.foo = function () {
console.log('Bar#foo called');
};
Bar.prototype.bar = function (arg) {
console.log(this);
console.log('Bar#bar called with ' + arg);
};
function Foo() {}
var Forwardble = {
forward: function() {
var methods = Array.prototype.slice.call(arguments);
var sender = this;
return {
to: function(receiver) {
for (var i=0; i<methods.length; i++) {
(function() {
var method = methods[i];
sender[method] = function() {
receiver[method].apply(receiver, arguments);
};
})();
}
}
}
}
}
// Extend?
Foo.prototype.forward = Forwardble.forward;
// ----------------------------------------------------------------------------
var foo = new Foo();
var bar = new Bar();
foo.forward('foo', 'bar').to(bar);
foo.foo();
foo.bar('baz!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment