// console aliases and verbose logger - console doesnt prototype
var c = console;
c.l = c.log,
c.e = c.error,
c.v = c.verbose = function() {
if (!myclass || !myclass.verbose) // verbose switch
return;
var args = Array.prototype.slice.call(arguments); // toArray
args.unshift('Verbose:');
c.l.apply(this, args); // log
};
// you can then do
var myclass = new myClass();
myclass.prototype.verbose = false;
// generally these calls would be inside your class
c.v('1 This will NOT log as verbose == false');
c.l('2 This will log');
myclass.verbose = true;
c.v('3 This will log');
我注意到,nitesh上述使用Array.prototype.unshift.call的是一種更好的方式來添加「詳細:」標籤。
爲什麼要擴展原型對象而不是擴展'Console'對象本身?你有沒有打電話給'新的控制檯()'? – biziclop 2012-02-14 13:43:17