IE兼容forEach/map/every/some/indexOf/filter
some
if (!Array.prototype.some){
  Array.prototype.some = 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 thisArg = arguments.length >= 2 ? arguments[1] : void 0;
    for (var i = 0; i < len; i++)
    {
      if (i in t && fun.call(thisArg, t[i], i, t))
        return true;
    }
    return false;
  };
}
every
if (!Array.prototype.every)
{
Array.prototype.every = 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 thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++)
{
if (i in t && !fun.call(thisArg, t[i], i, t))
return false;
} return true;
};
}
map
// Production steps of ECMA-262, Edition 5, 15.4.4.19
// Reference: http://es5.github.io/#x15.4.4.19
if (!Array.prototype.map) { Array.prototype.map = function(callback, thisArg) { var T, A, k; if (this == null) {
throw new TypeError(' this is null or not defined');
} // 1. Let O be the result of calling ToObject passing the |this|
// value as the argument.
var O = Object(this); // 2. Let lenValue be the result of calling the Get internal
// method of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0; // 4. If IsCallable(callback) is false, throw a TypeError exception.
// See: http://es5.github.com/#x9.11
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
} // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
if (arguments.length > 1) {
T = thisArg;
} // 6. Let A be a new array created as if by the expression new Array(len)
// where Array is the standard built-in constructor with that name and
// len is the value of len.
A = new Array(len); // 7. Let k be 0
k = 0; // 8. Repeat, while k < len
while (k < len) { var kValue, mappedValue; // a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty internal
// method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) { // i. Let kValue be the result of calling the Get internal
// method of O with argument Pk.
kValue = O[k]; // ii. Let mappedValue be the result of calling the Call internal
// method of callback with T as the this value and argument
// list containing kValue, k, and O.
mappedValue = callback.call(T, kValue, k, O); // iii. Call the DefineOwnProperty internal method of A with arguments
// Pk, Property Descriptor
// { Value: mappedValue,
// Writable: true,
// Enumerable: true,
// Configurable: true },
// and false. // In browsers that support Object.defineProperty, use the following:
// Object.defineProperty(A, k, {
// value: mappedValue,
// writable: true,
// enumerable: true,
// configurable: true
// }); // For best browser support, use the following:
A[k] = mappedValue;
}
// d. Increase k by 1.
k++;
} // 9. return A
return A;
};
}
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];
                if (fun.call(thisArg, val, i, t))
                res.push(val);
            }
        }
        return res;
    };
}
indexOf
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}
forEach
// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.io/#x15.4.4.18
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(fun /*, thisp*/){
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++){
if (i in this)
fun.call(thisp, this[i], i, this);
}
};
}
IE兼容forEach/map/every/some/indexOf/filter的更多相关文章
- JS数组filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()实例
		<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ... 
- 数组方法map(映射),reduce(规约),foreach(遍历),filter(过滤)
		数组方法map(映射),reduce(规约),foreach(遍历),filter(过滤) map()方法返回一个由原数组中每一个元素调用一个指定方法后返回的新数组 reduce()方法接受一个函数作 ... 
- JS数组中every(),filter(),forEach(),map(),some()方法学习笔记!
		ES5中定义了五种数组的迭代方法:every(),filter(),forEach(),map(),some(). 每个方法都接受两个参数:要在每一项运行的函数(必选)和运行该函数的作用域的对象-影响 ... 
- 【原】javascript笔记之Array方法forEach&map&filter&some&every&reduce&reduceRight
		做前端有多年了,看过不少技术文章,学了新的技术,但更新迭代快的大前端,庞大的知识库,很多学过就忘记了,特别在项目紧急的条件下,哪怕心中隐隐约约有学过一个方法,但会下意识的使用旧的方法去解决,多年前ES ... 
- js中数组的循环与遍历forEach,map
		对于前端的循环遍历我们知道有 针对js数组的forEach().map().filter().reduce()方法 针对js对象的for/in语句(for/in也能遍历数组,但不推荐) 针对jq数组/ ... 
- 高阶函数及 map、reduce、filter 的实现
		博客地址:https://ainyi.com/85 2020 开年国家经历了不少困难,最为凶猛的局势就是新型冠状病毒的蔓延,国务院最终决定春节假期延长至==2 月 2 号==:公司决定 3 - 7 号 ... 
- Python【map、reduce、filter】内置函数使用说明(转载)
		转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ... 
- 【转】Python 中map、reduce、filter函数
		转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ... 
- 转:Python一些特殊用法(map、reduce、filter、lambda、列表推导式等)
		Map函数: 原型:map(function, sequence),作用是将一个列表映射到另一个列表, 使用方法: def f(x): return x**2 l = range(1,10) map( ... 
随机推荐
- 根据键盘调整textField(多个)位置使其不会被键盘挡住
			当一个界面上有个textField时,键盘出现时需要保证textField不会被键盘挡住. 一般的做法是,监听 UIKeyboardWillShowNotification和 UIKeyboardWi ... 
- Unable to execute dex: Multiple dex files define(错误分析)
			eclipse工程包名与依靠的代码库包名不能冲突,否则运行程序程序会出错 错误提示: 
- jQuery删除节点和追加节点
			for (var i in checkedBoxIds) { var $td = $("#" + checkedBoxIds[i]).parent().parent().detac ... 
- eclipse编码格式设置
			大家好,我是小Alan,很高兴大家能够看到这篇小小的技术点文章,这还是从参加工作以来,小Alan写的第一篇博文.喜欢能够给一些朋友带来方便. 说到eclipse编码格式的设置其实一个非常非常小的事情, ... 
- WPF学习之路(二) XAML
			在WPF中引入了XAML语言,主要用于界面设计,业务逻辑则使用C#实现后台代码,将界面设计与业务逻辑分离 XAML是一种声明式语言,类似XML\HTML 示例: <!--Start Tag--& ... 
- 给现有MVC 项目添加 WebAPI
			1. 增加一个WebApi Controller, VS 会自动添加相关的引用,主要有System.Web.Http,System.Web.Http.WebHost,System.Net.Http 2 ... 
- Review 代码
			最近需要 Review 代码,学习了<代码整洁之道>.<代码质量>等书籍. 把对这些代码之道的学习心得整理成文 
- Linux hostname对Oracle实例以及监听的影响
			在Linux平台中,对hostname的修改,是否对ORACLE数据库实例或监听进程有影响呢?如果有影响,又要如何解决问题呢?另外/etc/hosts下相关内容的修改,是否也会影响实例或监听呢?这里涉 ... 
- [Linux 性能检测工具]TOP
			TOP NAME 显示linux任务 语法 top -hv | -abcHimMsS -d delay -n iterations -p pid [, pid ...] 描述 top程序提供了系统实时 ... 
- R语言中的循环函数(Grouping Function)
			R语言中有几个常用的函数,可以按组对数据进行处理,apply, lapply, sapply, tapply, mapply,等.这几个函数功能有些类似,下面介绍下这几个函数的用法. Apply 这是 ... 
