您好,欢迎来到高储教育。
搜索
您的当前位置:首页javascript解决IE8不支持filter的方法

javascript解决IE8不支持filter的方法

来源:高储教育


filter 被添加到 ECMA-262 标准第 5 版中,因此在某些实现环境中不被支持。可以把下面的代码插入到脚本的开头来解决此问题,该代码允许在那些没有原生支持 filter 的实现环境中使用它。

if (!Array.prototype.filter)
{
 Array.prototype.filter = function(fun /*, thisArg */)
 {
 "use strict";

 if (this === void 0 || this === null)
 throw new TypeError();

 var t = Object(this);
 var len = t.length >>> 0;
 if (typeof fun !== "function")
 throw new TypeError();

 var res = [];
 var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
 for (var i = 0; i < len; i++)
 {
 if (i in t)
 {
 var val = t[i];

 // NOTE: Technically this should Object.defineProperty at
 // the next index, as push can be affected by
 // properties on Object.prototype and Array.prototype.
 // But that method's new, and collisions should be
 // rare, so use the more-compatible alternative.
 if (fun.call(thisArg, val, i, t))
 res.push(val);
 }
 }

 return res;
 };
}

Copyright © 2019- gchn.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务