在Machina.js中传递变量

时间:2015-02-12 09:40:15

标签: javascript node.js machina.js

我试图掌握machina.js的运作方式。

我已确定state1执行自己的操作,并生成变量someVar,然后转换为state2

如何将someVar作为参数传递给state2

var fsm = new machina.Fsm({
    initialState: "uninitialized",
    states: {
        uninitialized: {},
        state1: {
            _onEnter: function() {
                var someVar = "..."; // Stuff.
                this.transition("state2");
            }
        },
        state2: {
            _onEnter: function() {
                // someVar as argument somehow...?
                console.log(someVar);
            }
        }
    }
});

1 个答案:

答案 0 :(得分:0)

你可能会这样做:

function FiniteStateMachineSetup() {
    this.someVar = null;
    this.fsm = new machina.Fsm({
        initialState: "uninitialized",
        states: {
            uninitialized: {},
            state1: {
                _onEnter: state1_onEnter.bind(null, this)
            },
            state2: {
                _onEnter: state2_onEnter.bind(null, this)
            }
        }
    });
}

function state1_onEnter(self) {
    self.someVar = "..."; // Stuff.
    self.fsm.transition("state2");
}

function state2_onEnter(self) {
    // someVar as argument somehow...?
    console.log(self.someVar);
}

然后您通过以下方式调用代码:

FiniteStateMachineSetup();