在Javascript中更正“静态”类型对象的结构

时间:2016-08-29 15:11:23

标签: javascript design-patterns properties scope static

我很难掌握在JavaScript中创建“静态”对象类型的问题(我怀疑我的措辞是否正确......)。

在下面的代码中,我试图创建一个jQuery插件(我怀疑这是无关紧要的),它创建了多个CrmQuery,每个都有自己的上下文信息传递为instance。我还想为所有CrmQuery创建一个“静态”对象(它可以是无状态的),并且希望将它包含在CrmQuery命名空间中。

这是我的结构,但在调用CrmQuery.searchOperator[instance.Operator].buildCondition(instance.fieldName, searchTerm)时,buildCondition函数无法访问oDataOperator中的CrmQuery.searchOperator函数。我尝试过以多种方式引用它,而不只是searchOperator.oDataOperator,而this是对实际属性的引用,而不是CrmQuery.searchOperator

结构:

(function($) { 
...
var crmQuery = new CrmQuery(instance);
var data = crmQuery.searchCrm(searchTerm);
... 

var CrmQuery = function(instance) {
    ...
    this.oDataUrl = function() {
        ...
        oDataFilter += CrmQuery.searchOperator[instance.Operator].buildCondition(instance.fieldName, searchTerm);
        ...
        return oDataFilter;
    }();

    this.searchCrm(searchTerm) {
        ...
        url += this.oDataUrl;
    }
    ...
}

CrmQuery.searchOperator = {
    oDataFunctionOperator: function oDataFunctionOperator(fieldName, searchTerm, operator) {
        return operator + "(" + fieldName + ", '" + searchTerm + "')";
    },
    oDataOperator: function oDataOperator(fieldName, searchTerm, operator) {
        return fieldName + " " + operator + " '" + searchTerm +"'";
    },
    STARTSWITH: { 
        operator: "startswith", 
        buildCondition: function(fieldName, searchTerm) { 
                return this.oDataFunctionOperator(fieldName, searchTerm, this.operator) 
            }
    },
    ENDSWITH: { 
        operator: "endswith", 
        buildCondition: function(fieldName, searchTerm) { 
                return searchOperator.oDataFunctionOperator(fieldName, searchTerm, this.operator) 
            }
    },
    CONTAINS: { 
        operator: "substringof", 
        buildCondition: function(fieldName, searchTerm) { 
                return searchOperator.oDataFunctionOperator(fieldName, searchTerm, this.operator) 
            }
    },
    EXACTMATCH: { 
        operator: "eq", 
        buildCondition: function(fieldName, searchTerm) { 
                return searchOperator.oDataOperator(fieldName, searchTerm, this.operator) 
            }
    },
    toString: function () {
        var thisVar = this;
        var output = "searchOperator enum. ";
        output += $.map(Object.keys(this), function (i) {
            if (typeof thisVar[i] !== "function") 
                return i + ": " + thisVar[i].operator;
        }).join(", ");
        return output;
    }
}
...
}(jQuery));

在JavaScript中实现这样的模式的正确方法是什么?

我希望这有道理吗?如果它太冗长,我会道歉,但我不想留下任何重要的东西。

非常感谢, 詹姆斯

1 个答案:

答案 0 :(得分:0)

继承是解决此问题的一种方法,例如:

function searchOperator() {
}

//searchOperator.prototype.toString = ...

functionOperator = function(operator) {
    searchOperator.apply(this);
    this.operator = operator;
}

functionOperator.prototype = Object.create(searchOperator.prototype);

functionOperator.prototype.buildCondition = function (fieldName, searchTerm) {
    return this.operator + "(" + fieldName + ", '" + searchTerm + "')";
}

operators = {

    STARTSWITH: new functionOperator('startswith')
}

console.log(operators.STARTSWITH.buildCondition('foo', 'bar'))