I want to delegate several methods from one JavaScript object to another. So I thought about using metaprogramming so that several methods were not defined just like delegates. So far I have finished with this method:
function delegate_to(_method, _obj) { return function(_args) {
So, as an example code, how it should work:
var that = {} var delegate = {} that.foo = function(_message) { console.log("foo: " + _message) } that.bar = function(_message) { console.log("bar: " + _message) } that.baz = function(_message) { console.log("baz: " + _message) } function delegate_to(_method, _obj) { return function(_args) {
The code really works, but what if I want to delegate a method that has several parameters? What about the parameters n ? Can I change the code to any number of parameters? Does it work in any browser?
Regards, Rainer
source share