views:

434

answers:

1

I'm writing a quick log function to wrap around the native console.log to prevent errors later on in development (like forgotten console.log statements in your code). I'm using the mootools $defined and $type functions to check for the existence of a console and the console.log function before calling it. However when I disable firebug I get the following error in firefox.

Error: console is not defined
Source File: http://diagnostic.localhost/js/emp.custom.js
Line: 6

EMP.log = function() {
 if (DEBUG && $defined(console) && $type(console.log) == 'function') { //line 6
  var args = Array.prototype.slice.call(arguments); //turn arguments into array
  console.log.pass(args);
 }
}

It seems like using $defined should eliminate this error, so does anyone have any ideas as to what the issue may be?

I'm using mootools v1.2.3.

EDIT: I've tried the following and they also give them same error:

if (DEBUG && $type(console) == "object" && $type(console.log) == 'function') {

if (DEBUG && $chk(console) && $type(console.log) == 'function') {
+4  A: 

Try $defined(window.console) instead. If Firefox goes through the scope chain and can't find a variable, it'll throw an error, but if you're explicit with the context where you're looking for a variable, it'll give you undefined.

Tmdean
Thanks for that. Works great now.
tj111
Nice! Glad you've figured it out :)
Kai