淘汰不评估IE7中的表达式

use*_*251 5 javascript internet-explorer internet-explorer-7 knockout.js

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

Mozilla的结果

value 1
value 2
value 3
Run Code Online (Sandbox Code Playgroud)

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;       
   }
}
Run Code Online (Sandbox Code Playgroud)

如何在IE7中正常工作?

Rup*_*ups 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;
        };
    }
Run Code Online (Sandbox Code Playgroud)