//I have the following function:
function handle_message(msg)
{
//do work
console.log('some work: '+msg.val);
//call next message
msg.next();
}
//And array of message objects:
var msgs = [ {val : 'first msg'}, { val : 'second msg'}, { val : 'third msg'}];
//I link messages by setting next parameter in a way that it calls handle_message for the next msg in the list. Last one displays alert message.
msgs[2].next = function() {alert('done!')};
msgs[1].next = function() {handle_message(msgs[2]);};
msgs[0].next = function() {handle_message(msgs[1]);};
//Start the message handle "chain". It works!
handle_message(msgs[0]);
//======== Now I do exactly the same thing but I link messages using the for loop:
for (var i=msgs.length-1; i>=0; i--)
{
if (i==msgs.length-1)
{
msgs[i].next = function() {alert('done!');};
}
else
{
msgs[i].next = function() {handle_message(msgs[i+1]);};
}
}
//Start the message handling chain. It fails! It goes into infinite recursion (second message calls itself)
handle_message(msgs[0]);
能夠解釋它爲什麼會發生?或者,也許這種模式的替代?我的情況是這樣的:我收到一個包含消息的數組,我必須按順序處理它們,另一個同步地處理它們。問題是一些消息需要發射一系列動畫(jqwuery animate(),這是異步的),並且在最後一個動畫完成之前無法處理以下消息。由於JavaScript中沒有sleep(),我試圖在消息完成後調用下一個消息的情況下使用這種模式(在動畫的情況下,我只是將'next'函數指針傳遞給動畫的「complete」回調函數)。無論如何,我想動態構建這個「鏈」,但發現了這個奇怪的(?)行爲。閉環中的javascript關閉
你應該真的在爲這個類使用類,用原型對象。 – RobertPitt 2011-04-17 19:49:55
這是一個非常糟糕的問題標題。今後,請嘗試總結一下您看到的讓您感到困惑的結果,或者您嘗試實現的結果。你不妨發佈一個題爲「我很困惑」或「幫助!」的問題。 – Phrogz 2011-04-17 19:56:40
你說得對,我改變了它。 – PawelRoman 2011-04-17 20:05:01