将这个例子作为原型函数实现的技术和含义?

时间:2017-03-06 19:40:45

标签: javascript

我在js中创建了几个方差颜色查找对象。不同的元素将具有不同的方差颜色:

    var varianceColorTable =
    {
        Ranges:
        [
            //decrease
            { From: -10000, To: -1.01, Color: '#595959' },
            { From: -1.00, To: -0.01, Color: '#999999' },
            //no change
            { From: 0.00, To: 0.00, Color: '#FFFFFF' },
            //increase
            { From: 0.01, To: 1.00, Color: '#BABC16' },
            { From: 1.01, To: 10000, Color: '#576A2D' }
        ]
    }

    return getColor(varianceColorTable, range);

    function getColor()
    {
        var rangeColor = $.grep(varianceColorTable.Ranges, function (n, i) {
            return variance >= n.From && variance <= n.To;
        })[0].Color;
    }

此代码在VarianceColorLookupManager.js模块中定义。我可以在此模块中定义原型函数,因此getColor(range)将是对象的一个​​函数吗?例如:

    return {
        Ranges:
        [
            //decrease
            { From: -10000, To: -1.01, Color: '#595959' },
            { From: -1.00, To: -0.01, Color: '#999999' },
            //no change
            { From: 0.00, To: 0.00, Color: '#FFFFFF' },
            //increase
            { From: 0.01, To: 1.00, Color: '#BABC16' },
            { From: 1.01, To: 10000, Color: '#576A2D' }
        ]
    }.getColor(varianceColorTable, range);

如果可以,那么可以将js模块的getColor()对象原型函数置于本地,如果它还没有默认吗?

0 个答案:

没有答案