Skip to content

Commit

Permalink
Create stubbed console methods
Browse files Browse the repository at this point in the history
Remove the previous `log()` wrapper method as it changed the reported
line position in logs. Instead, avoid `console` errors in browsers
without `console` by setting its methods to empty functions.

Ref. #1107.
  • Loading branch information
mathiasbynens authored and necolas committed Jul 8, 2012
1 parent 83f4f28 commit 5370479
Showing 1 changed file with 11 additions and 42 deletions.
53 changes: 11 additions & 42 deletions js/plugins.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,14 @@
// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/

window.log = function f() {
log.history = log.history || [];
log.history.push(arguments);
if (this.console) {
var args = arguments;
var newarr;

try {
args.callee = f.caller;
} catch(e) {

}

newarr = [].slice.call(args);

if (typeof console.log === 'object') {
log.apply.call(console.log, console, newarr);
} else {
console.log.apply(console, newarr);
// Avoid `console` errors in browsers that lack a console
if (!(window.console && console.log)) {
(function() {
var noop = function() {};
var methods = ['assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'markTimeline', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn'];
var length = methods.length;
var console = window.console = {};
while (length--) {
console[methods[length]] = noop;
}

This comment has been minimized.

Copy link
@meeDamian

meeDamian Jul 24, 2012

Why not just:

var noop = function(){},
    console = window.console = {},
    methods = ['assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'markTimeline', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn'];
for(var i in methods) console[ methods[i] ]=noop;    

This comment has been minimized.

Copy link
@mathiasbynens

mathiasbynens Jul 24, 2012

Author Member

@chester1000 Using for-in is not a good way to iterate over arrays. You could argue that in this case it doesn’t really matter as the order is not really important, but since it’s a best practice to avoid it we’ll just use the code we have right now.

This comment has been minimized.

Copy link
@meeDamian

meeDamian Jul 24, 2012

Ok, matter of principle, I get it (: . Any more info about why using for-in is not recommended?

This comment has been minimized.

This comment has been minimized.

Copy link
@meeDamian

meeDamian Jul 24, 2012

+1 and T.Hanks (;

}
};

// make it safe to use console.log always

(function(a) {
function b() {}
var c = "assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn";
var d;
for (c = c.split(","); !!(d = c.pop());) {
a[d] = a[d] || b;
}
})(function() {
try {
console.log();
return window.console;
} catch(a) {
return (window.console = {});
}
}());
}());
}

// place any jQuery/helper plugins in here, instead of separate, slower script files.

0 comments on commit 5370479

Please sign in to comment.