How about this?
var Toolbar = { init: function(toolbar) { this.Bar = $(toolbar); // scope is Toolbar object literal this.trigger = function() { this.Bar.trigger.apply(this.Bar, arguments); }; this.bind = function() { this.Bar.bind.apply(this.Bar, arguments); }; this.Bar.find('clearButton').click(function() { this.trigger('clear'); } }; Toolbar.init(); Toolbar.bind('clear', function() { ... });
If you want, you can easily create a function to handle packaging; any of them, as you prefer:
function wrapperitize(wrapper, wrappee, method /*, more_methods...*/) { wrapper[method] = function() { wrappee[method].apply(wrappee, arguments); }; for(var i = 3; i < arguments.length; ++i) wrapperitize(wrapper, wrappee, arguments[i]); } function wrapperitize(wrapper, wrappeeProp, method /*, more_methods...*/) { wrapper[method] = function() { wrapper[wrappeeProp][method].apply(wrapper[wrappeeProp], arguments); }; for(var i = 3; i < arguments.length; ++i) wrapperitize(wrapper, wrappeeProp, arguments[i]); }
where the former will be called as wrapperitize(this, this.Bar, 'trigger', 'bind')
, and the latter will be called wrapperitize(this, 'Bar', 'trigger', 'bind')
(the difference is what the former will do this
is a wrapper for any this.Bar
for the time being, while the latter will make this
a wrapper for any this.Bar
, possibly sometime in the future.
(Note, by the way, a tiny bit of recursion. This is done to avoid the problem of capturing variables due to how JavaScript locks work.)