我试图掌握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);
            }
        }
    }
});

最佳答案

您可能会执行以下操作:

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();

关于javascript - 在Machina.js中传递变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28474012/

10-11 04:33