【发布时间】:2011-10-20 09:51:11
【问题描述】:
我正在使用 Google Closure Library 及其编译器创建一个应用程序。要调试值,我使用console.log()。编译它会抛出以下异常JSC_UNDEFINED_VARIABLE. variable console is undeclared at ...。为了解决这个错误,我只需要改用window.console.log()。
我还想测量一个函数所花费的时间。 Firebug 有两个很好的函数console.time(name) 和console.timeEnd(name) 可以很容易地做到这一点。不幸的是,闭包编译器不支持这些函数并抛出以下警告JSC_INEXISTENT_PROPERTY. Property time never defined on Window.prototype.console at ...。不幸的是,您无法通过添加 window 来解决此警告。
我还查看了库,但 goog.debug.Console 没有我需要的功能。
我之前使用的另一个解决方案类似于以下
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);
代码有点多,如果你经常使用的话,有两个功能的版本会很棒:
window.console.time("timeTaken");
// do something
window.console.timeEnd("timeTaken");
这会打印出开始和结束之间的 MS。但如上所述,这不适用于闭包编译器。有没有人对此有解决方案,我如何使用这两个功能window.console.time() 和window.console.timeEnd()?或者 goog.closure 提供的另一种解决方案,但我还没有找到?
【问题讨论】:
标签: google-closure-compiler google-closure google-closure-library