4

I'm creating an application with Google Closure Library and its Compiler. To debug values I use console.log(). Compiling this will throw the following exception JSC_UNDEFINED_VARIABLE. variable console is undeclared at .... To solve this error, I just had to use window.console.log() instead.
I also want to measure the time that a function takes. Firebug has two nice functions console.time(name) and console.timeEnd(name) to do that very easily. Unfortunately the Closure Compiler does not support these functions throwing the following warning JSC_INEXISTENT_PROPERTY. Property time never defined on Window.prototype.console at .... Unfortunately you cannot solve this warning with prepending window.
I also had a look at the library, but goog.debug.Console has not the function that I need.
Another solution I have used before was something like the following

var start = new Date();  
// do something
var end = new Date();

// do some calculation to get the ms for both start and end
var startMS = ....;
var endMS = .....;
// get the difference and print it
var difference = (endMS - startMS) / 1000;
console.log('Time taken for something: ' + difference);

This is a little bit too much code, if you use it very often, and the version with the two functions would be great:

window.console.time("timeTaken");
// do something
window.console.timeEnd("timeTaken");

This prints out the MS between start and end. But as mentioned above, this doesn't work with the closure compiler. Does anyone have a solution for this, how I can use these two functions window.console.time() and window.console.timeEnd()? Or maybe another solution that goog.closure provides, but I haven't found?

Gerrit
  • 174
  • 2
  • 11

2 Answers2

4

You just need to added them to the externs you are using.

John
  • 5,443
  • 15
  • 21
  • 1
    Thank you for the advice. While trying to declare the firebug extension and having a lot of errors (Firebug uses the CONST keyword, that is not allowed with the Closure Compiler), I found in the directory of the Compiler the _webkit_console.js_, in which both functions are declared. – Gerrit Oct 21 '11 at 07:54
  • yeah, "const" isn't part of the standard thus isn't supported. We have to wait for EcmaScript 6 for that. Personally, I can't wait. "/** @const */" isn't nearly as likely to be used. – John Nov 04 '11 at 00:59
  • 1
    We did add a "--accept_const_keyword" option to make things simpler for people, this simply rewrites "const" as "var" for the rest of the compiler. – John May 07 '12 at 23:02
  • I'm not very familiar with Closure - could you give an example of the extern declaration for `console`? – poolie Dec 12 '12 at 04:17
  • minimally: var console; console.time; console.timeEnd; You can use the existing externs as to get a general feel of things: https://code.google.com/p/closure-compiler/source/browse/trunk/externs/es3.js – John Dec 12 '12 at 06:40
  • https://code.google.com/searchframe#l5BkQmivP-Y/trunk/contrib/externs/webkit_console.js – jedierikb Feb 05 '13 at 21:53
  • Both links above are dead, any other sources? – stephen mc Nov 06 '13 at 16:00
1

If you don't want to/can't use externs, you can easily reference "undeclared" objects with the string-based properties:

window['console']['log']('Hello Console!');
window['console']['time']('timeTaken');
...

But you have to be careful, because the second line might throw an error if the time property does not exist or it's not a function.

petr
  • 179
  • 9