This file contains hidden or 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
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:
This file contains hidden or 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
//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.