Google Closure Library とそのコンパイラを使用してアプリケーションを作成しています。値をデバッグするには、 を使用しますconsole.log()
。これをコンパイルすると、次の例外がスローされますJSC_UNDEFINED_VARIABLE. variable console is undeclared at ...
。このエラーを解決するには、window.console.log()
代わりに使用する必要がありました。
関数にかかる時間も測定したいと思います。Firebug には 2 つの優れた機能がconsole.time(name)
ありconsole.timeEnd(name)
、それを非常に簡単に行うことができます。残念ながら、Closure Compiler はこれらの関数をサポートしておらず、次の警告がスローされますJSC_INEXISTENT_PROPERTY. Property time never defined on Window.prototype.console at ...
。残念ながら、この警告を prepending で解決することはできません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);
非常に頻繁に使用する場合、これは少し多すぎるコードであり、2 つの機能を備えたバージョンが最適です。
window.console.time("timeTaken");
// do something
window.console.timeEnd("timeTaken");
これにより、開始と終了の間の MS が出力されます。しかし、前述のように、これはクロージャ コンパイラでは機能しません。window.console.time()
これらの2つの機能をどのように使用できますwindow.console.timeEnd()
か? それとも、 goog.closure が提供する別の解決策が見つかりませんか?