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

// 获取元素的值
                return ele.firstChild.nodeValue;
            }
        };

/**
         * 属性作为终结符对应的解释器
         * @param {String} propName [属性的名称]
         */
        function PropertyTerminalExpression(propName) {
            this.propName = propName;
        }

PropertyTerminalExpression.prototype = {
            interpret: function (context) {
                // 直接获取最后的元素属性的值
                return context.getPreEles()[0].getAttribute(this.propName);
            }
        };

/**
         * 多个属性作为终结符对应的解释器
         * @param {String} propName [属性的名称]
         */
        function PropertysTerminalExpression(propName) {
            this.propName = propName;
        }

PropertysTerminalExpression.prototype = {
            interpret: function (context) {
                var eles = context.getPreEles();
                var ss = [];

for (var i = 0, len = eles.length; i < len; i++) {
                    ss.push(eles[i].getAttribute(this.propName));
                }

return ss;
            }
        };

/**
         * 以多个元素作为终结符的解释处理对象
         * @param {[type]} name [description]
         */
        function ElementsTerminalExpression(name) {
            this.eleName = name;
        }

ElementsTerminalExpression.prototype = {
            interpret: function (context) {
                var pEles = context.getPreEles();
                var nowEles = [];

for (var i = 0, len = pEles.length; i < len; i++) {
                    nowEles = nowEles.concat(context.getNowEles(pEles[i], this.eleName));
                }

var ss = [];

for (i = 0, len = nowEles.length; i < len; i++) {
                    ss.push(nowEles[i].firstChild.nodeValue);
                }

return ss;
            }
        };

/**
         * 多个元素作为非终结符的解释处理对象
         */
        function ElementsExpression(name) {
            this.eleName = name;
            this.eles = [];
        }

ElementsExpression.prototype = {
            interpret: function (context) {
                var pEles = context.getPreEles();
                var nowEles = [];

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

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