Skip to content

Instantly share code, notes, and snippets.

@zhanhongtao
Created October 10, 2013 09:07
Show Gist options
  • Save zhanhongtao/6915372 to your computer and use it in GitHub Desktop.
Save zhanhongtao/6915372 to your computer and use it in GitHub Desktop.
console.log
var type = function(s) {
return Object.prototype.toString.call(s).slice(8,-1).toLowerCase();
};
var log = function() {
var color = 'background: #000;color: yellow;padding: 0 3px';
var string = '%c';//'%c';
for ( var i = 0, l = arguments.length; i < l; i++ ) {
var t = type(arguments[i]);
switch(t) {
case 'string':
string += '%s';
break;
case 'number':
string += ( '' + arguments[i] ).indexOf('.') > -1 ? '%f' : '%d';
break;
case 'object':
string += '%O';
break;
default:
if ( t.indexOf('html') > -1 ) {
string += '%o';
}
else {
string += '%s';
}
break;
}
}
var argus = [].concat.apply( [ string, color ], arguments );
console.log.apply( console, argus );
};
log( null, undefined, 'string', 1, 1.2, [1], {a:'a',b:'b'}, document.body);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment