best way in javascript to make console.log calls not cause problems when there's no console?
I'm using firebug and making lots of console.log, .info, .dir calls, etc. When the app runs on a ma开发者_高级运维chine with firebug turned off, it can cause errors. What's the best technique to avoid that? This seems to work:
// global scope
if (typeof(console) == 'undefined') {
console = {
info : function() {},
dir : function() {},
error : function() {},
log : function() {}
};
}
but I don't like the idea of manually maintaining a list of console functions. Other ideas?
(We've also got jQuery on the project if that helps.)
I personally just use $.noop
to shorten it like this:
if(!window.console)
window.console = { log: $.noop, group: $.noop, groupEnd: $.noop };
But whatever functions you're using, add them in.
精彩评论