javascript设计模式之解释器模式详解(7)

if (!pEles.length) {
                    // 说明现在获取的是根元素
                    ele = context.getDocument().documentElement;
                    pEles.push(ele);
                    context.setPreEles(pEles);
                } else {
                    var tempEle;
                    for (var i = 0, len = pEles.length; i < len; i++) {
                        tempEle = pEles[i];
                        nowEles = nowEles.concat(context.getNowEles(tempEle, this.eleName));

// 找到一个就停止
                        if (nowEles.length) break;
                    }

context.setPreEles([nowEles[0]]);
                }

var ss;
                // 循环调用子元素的interpret方法
                for (var i = 0, len = this.eles.length; i < len; i++) {
                    ss = this.eles[i].interpret(context);
                }

return ss;
            }
        };

/**
         * 元素作为终结符对应的解释器
         * @param {String} name [元素的名称]
         */
        function ElementTerminalExpression(name) {
            this.eleName = name;
        }

ElementTerminalExpression.prototype = {
            interpret: function (context) {
                var pEles = context.getPreEles();
                var ele = null;
                if (!pEles.length) {
                    ele = context.getDocument().documentElement;
                } else {
                    ele = context.getNowEles(pEles[0], this.eleName)[0];
                }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgpjfd.html