« »
6/16/2010

How to make Firebug methods chainable.

I just wrote this piece of code (mainly for fun) which let you use Firebug methods with chained calls.

var makeChainable = function(obj, method) {
var m = Object.prototype.toString.call(method) === "[object Array]" ? method: [method],
i = m.length;
while (i--) {
obj[m[i]] = function(o) {
return function() {
o.apply(this, arguments);
return this;
}
} (obj[m[i]]);
};
}


Usage:

//Do this only one time !
makeChainable(console, 'assert clear count debug dir dirxml error exception group groupCollapsed groupEnd info log profile profileEnd time timeEnd warn'.split(' '));
//Usage:
console
.profile()
.time('testTime')
.group('GroupTest')
.debug('test', 1, 2, 3)
.warn('warniiing!')
.log('loo', true)
.dir([1, 2, 3, 4])
.info('plop')
.groupEnd('GroupTest')
.timeEnd('testTime')
.profileEnd();


Ps: If you want to create chainable methods, just add "return this;" at each methods end. makeChainable is only usefull with already created objects.
« »
 
 
Made with on a hot august night from an airplane the 19th of March 2017.