JavaScript and finite state machine learning

Source: Internet
Author: User
Tags fsm

A finite state machine (finite-state machine) is a very useful model for simulating most things in the world.

Simply put, it has three features:

* The total number of States (state) is limited.

* At any one time, only in a state.

* Under certain conditions, from a state transition (transition) to another state.

What it means to JavaScript is that many objects can be written in a finite state machine.

For example, there is a menu element on a Web page. When the mouse hovers, the menu is displayed, and when the mouse moves away, the menu is hidden. If you use a finite state machine description, the menu has only two states (show and hide), and the mouse triggers a state transition.

The code can be written in the following way:

var menu = {
        
//Current state
CurrentState: ' Hide ',
    
//binding event
Initialize:function () {
var self = thi    s;
Self.on ("hover", self.transition);
},
    
//state transition
transition:function (event) {
switch (this.currentstate) {case
"hide":
This.currentstate = ' show ';
DoSomething ();
break;
Case "Show":
this.currentstate = ' hide ';
DoSomething ();
break;
Default:
console.log (' Invalid state! ');
break;
}
}
    
;

It can be seen that the finite state machine writing, logic clear, strong expression, conducive to packaging events. The more the state of an object and the more events that occur, the more appropriate to use the finite state machine.

In addition, the JavaScript language is a very much asynchronous operation of the language, the common solution is to specify a callback function, but this will cause the code structure confusion, difficult to test and debug problems. The finite state machine provides a better way to hook up the asynchronous operation with the state of the object, and when the asynchronous operation ends, the corresponding state changes occur, triggering other actions. This is logically more logical and easier to reduce the complexity of the code than callback functions, event sniffing, publish/subscribe solutions.

The following describes a library of finite state machines, JavaScript, finite, and Machine. This library is very understood, it can help us to deepen understanding, and the function is not weak at all.

The library provides a global object statemachine that can be used to generate instances of a finite state machine using the Create method of the object.

var FSM = statemachine.create ();

When you build, you need to provide a parameter object that describes the nature of the instance. For example, a traffic light can be described like this:

var FSM = statemachine.create ({
    
initial: ' Green ',
    
events: [
{name: ' Warn ', from  : ' Green ', to  : ' Yellow '},
{name: ' Stop ', from: ' Yellow ', to: ' Red '},
{name: ' Ready ', from  : ' Red ', to    : ' Yello W '},
{name: ' Go ', from: ' Yellow ', to: ' Green '}
]
    
};

The initial state of traffic lights (initial) is the Green,events property is the trigger state changes of various events, such as Warn event makes Green state into yellow State, stop events make the yellow state red state and so on.

After the instance is generated, you can query the current state at any time.

* Fsm.current: Returns the current state.

* Fsm.is (s): Returns a Boolean value indicating whether the state S is the current state.

* Fsm.can (E): Returns a Boolean value indicating whether event E can be triggered in the current state.

* Fsm.cannot (E): Returns a Boolean value indicating whether event E cannot be triggered in the current state.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.