淘汰不评估IE7中的表达式

时间:2012-11-30 15:31:18

标签: javascript internet-explorer knockout.js internet-explorer-7

当我在Mozilla中运行我的程序时,它解析了knockout表达式并显示了observable数组中的值。当我在IE7中做同样的事情时,它显示了淘汰代码。

Mozilla结果

value 1
value 2
value 3

IE7结果

function observable() { 
   if (arguments.length > 0) {
        // Write
        // Ignore writes if the value hasn't changed
        if ((!observable['equalityComparer']) || !observable['equalityComparer'](_latestValue, arguments[0])) {
            observable.valueWillMutate();
            _latestValue = arguments[0];
            if (DEBUG) observable._latestValue = _latestValue;
            observable.valueHasMutated();            
        }
        return this; // Permits chained assignments        
   }        
   else {
        // Read
        ko.dependencyDetection.registerDependency(observable); // The caller only needs to be notified of changes if they did a "read" operation
        return _latestValue;       
   }
}

如何在IE7中正常工作?

1 个答案:

答案 0 :(得分:2)

IE浏览器不支持数组的indexOf,这会导致knockout.js框架出现问题。

添加以下javascript,它可能会解决您的问题:

    //
    // IE browsers do not support indexOf method for an Array. Hence 
    // we add it below after performing the check on the existence of
    // the same.
    //
    if (!Array.prototype.indexOf)
    {
        Array.prototype.indexOf = function (obj, start)
        {
            for (var i = (start || 0), j = this.length; i < j; i++)
            {
                if (this[i] === obj)
                {
                    return i;
                }
            }
            return -1;
        };
    }