js.pattern-h 상태 모드

2225 단어
'use strict';

let State = (function () {
    class State {
        constructor() {
            /**
             *  
             * @type {{}}
             * @private
             */
            let that = this;

            that._currState = {};
            that.states = {
                jump: function () {
                    console.log("jump")
                },
                move: function () {
                    console.log("move")
                },
                shoot: function () {
                    console.log("shoot")
                },
                squat: function () {
                    console.log("squat")
                }
            };
            /**
             *  
             * @type {{change, goes}}
             */
            let {change, goes} = (function () {
                class action {
                    constructor() {
                        this.change = function () {
                            // 
                            let arg = [].slice.call(arguments).sort();
                            that._currState = {};
                            if (arg.length) {
                                for (var i in arg) {
                                    that._currState[arg[i]] = true;
                                }
                            }
                            return that;
                        }

                        this.goes = function () {
                            console.log(" ")
                            for (var i  in that._currState) {
                                that.states[i] && that.states[i]()
                            }
                            return that;
                        }
                    }

                }
                let _action = new action();
                return {
                    change: _action.change,
                    goes: _action.goes
                }
            })();
            this.change = change;
            this.goes = goes;
        }
    }

    return new State();
})();

console.log(State.change('jump', 'shoot').goes().goes().change("move").goes());

좋은 웹페이지 즐겨찾기