2
私は、この機能を実行するために問題を抱えている私は、エラーを持っていませんが、何も起こりません、私はここ140 bytes Finite State Machine in JavaScriptからこの機能を取る:JavaScript FSM(Finite State Machine)この機能を実行するにはどうすればよいですか?
@param {文字列}ステートマシンは
を初期化される上状態ステートマシンのダイアグラムB @param {オブジェクト}
{ "state1": {
"event1": [action1, "state2"],
"event2": [action2]
},
"state2": {
"event3": [[action3, context], "state1"]
}
}
@returns {オブジェクト}
SM = function(
a // stores the current state
,b // an object to store all states and their transitions
){
return{
event:function( // The function to send an event to the state machine
c // The name of the event
,d // The arguments to pass to the action
){
console.log(c);
console.log(b[a][c]);
return (c=b[a][c]) // Save the array [action, nextState] in c which is carefuly reused,
&& ( // If c is defined.
(c[0][0]||c[0]) // Either c[0] is the function, or c[0][0] if a scope is given
.call(c[0][1],d), // call the function in the context or call it directly
a=c[1]||a // The next state is the new state and the new state is returned
)
}
}
}
function msg(x){
alert(x);
}
function msg1(x){
alert(x);
}
function msg2(x){
alert(x);
}
var context = 20;
b = { "state1": {
"event1": [msg, "state2"],
"event2": [msg1]
},
"state2": {
"event3": [[msg2, context], "state1"]
}
}
w=SM("state1", b);
w.event;