Created
July 10, 2014 09:44
-
-
Save polarblau/4e2e0acef7a325c261aa to your computer and use it in GitHub Desktop.
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 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