LoginSignup
8
6

More than 5 years have passed since last update.

javascript無名関数でハマった話

Posted at
// メインループ
var loop = function() {
...
setTimeout(arguments.callee, 1000 / 30);
}

// 準備ができたらloopを開始
(function(){
if (okay) loop();
else setTimeout(arguments.callee, 1);
})();

これundefined is not functionが出るんだけど、

var loop = function(){...}(...)();

と解釈されてた・・・。

正しくは

// メインループ
var loop = function() {
...
setTimeout(arguments.callee, 1000 / 30);
}; // 行末セミコロン

// 準備ができたらloopを開始
(function(){
if (okay) loop();
else setTimeout(arguments.callee, 1);
})();

あるいはこっちの構文だと問題ない。

// メインループ
function loop() {
...
setTimeout(arguments.callee, 1000 / 30);
} // 行末セミコロン不要

// 準備ができたらloopを開始
(function(){
if (okay) loop();
else setTimeout(arguments.callee, 1);
})();

気づかんわ!!

8
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6