JavaScript 实现数组的foreach
Array.prototype.forEach = function (action) {
for (var i = 0; i < this.length; i++) {
action(this[i]);
}
};
/**
* 以jqery 对象的方式迭代对象
* @param {} action
* @returns {}
*/
Array.prototype.forEachJq = function (action) {
for (var i = 0; i < this.length; i++) {
action($(this[i]));
}
};
-----------------------------------------------------------------
// 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(callback, thisArg) { var T, 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 k be 0 k = 0; // 7. Repeat, while k < len while (k < len) { var kValue; // 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. Call the Call internal method of callback with T as // the this value and argument list containing kValue, k, and O. callback.call(T, kValue, k, O); } // d. Increase k by 1. k++; } // 8. return undefined }; }
JavaScript 实现数组的foreach相关推荐
- 在 JavaScript 中对数组进行 for-each
问题描述: 如何使用 JavaScript 遍历数组中的所有条目? 解决方案1: huntsbot.com高效搞钱,一站式跟进超10+任务平台外包需求 TL;博士 您最好的选择通常是 for-of 循 ...
- JavaScript中的数组遍历forEach()与map()方法以及兼容写法
原理: 高级浏览器支持forEach方法 语法:forEach和map都支持2个参数:一个是回调函数(item,index,list)和上下文: forEach:用来遍历数组中的每一项:这个方法执行是 ...
- ES5 数组方法forEach
ES6已经到了非学不可的地步了,对于ES5都不太熟的我决定是时候学习ES5了. 1. js 数组循环遍历. 数组循环变量,最先想到的就是 for(var i=0;i<count;i++)这样的 ...
- 深入理解JavaScript类数组
起因 写这篇博客的起因,是我在知乎上回答一个问题时,说自己在学前端时把<JavaScript高级程序设计>看了好几遍. 于是在评论区中,出现了如下的对话: 天啦噜,这话说的,宝宝感觉到的, ...
- JavaScript中数组使用总结
一. 数组的概念 1.1 什么是数组 数组是指的数据的有序列表. 数组中每个值称之为数组的一个元素. 数组中的每个元素都有一个位置,这个位置称之为索引(下标.index).数组的索引是从 0 开始的 ...
- 九、玩转JavaScript的数组(五)
@Author:Runsen @Date:2020/5/26 作者介绍:Runsen目前大三下学期,专业化学工程与工艺,大学沉迷日语,Python, Java和一系列数据分析软件.导致翘课严重,专业排 ...
- JavaScript对数组的处理(一)
数组创建JavaScript中创建数组有两种方式,第一种是使用 Array 构造函数:var arr1 = new Array(); //创建一个空数组 var arr2 = new Array(20 ...
- javascript中数组的22种方法
前面的话数组总共有22种方法,本文将其分为对象继承方法.数组转换方法.栈和队列方法.数组排序方法.数组拼接方法.创建子数组方法.数组删改方法.数组位置方法.数组归并方法和数组迭代方法共10类来进行详细 ...
- foreach 循环达到条件的第一次就停止_是否应该停止在JavaScript代码中使用forEach()?...
在本文中,我想概述一下标准的 for 循环和 forEach() 方法背后的区别,并对它们各自带来的一些好处进行评论. 作为免责声明,请不要把标题当真.我写这篇文章的目的是为了让读者了解缺点,并提供一 ...
最新文章
- greta的问题及解决: LIBCMTD.lib(_wctype.obj) : error LNK2005: _iswspace already defined in xxx.obj...
- 理解python的with as 语句
- 节假日api_iOS能跳过节假日的晚安闹钟捷径
- Android之二维码扫描的总结
- NOIP模拟测试8「寿司」
- idea 单独引入jar_Intellij IDEA 添加jar包的三种方式
- python 编码 解码 读写文件
- python_标识符_帮助系统的使用_命名规则---python工作笔记018
- java lua传参_Lua将函数参数作为表键传递
- 宿主机docker容器互传文件命令
- IOS contentOffset该如何理解
- [转载]ASP.NET-----Repeater数据控件的用法总结
- DropDownList 数据绑定
- 怎么把文本文档变成html,如何将word文档转换成txt文本
- 中国地图经纬度json
- Excel游戏—制作数字炸弹小游戏
- 关于结婚照,还不如PS去~
- Excel 预习阶段Day1
- 谷歌分析相关套件简介
- 24C01SC芯片介绍
热门文章
- mac 下homebrew 的安装和卸载
- Java对异常处理或抛出之后,后面代码会不会再执行?
- ar 微信小程序_小程序可以实现AR效果了 微信还为开发者提供了基础能力支持
- ios怎么安装python3.7_Python3、PyCharm的安装及使用方法(Mac版)
- 【Java】练习题:蒙眼过马路
- MATLAB-循环类型相关(for while break continue)
- 关于linux内核描述正确的是 牛客网,在Linux系统,关于硬链接的描述正确的是()...
- javascript变量提前声明
- php 类 静态调用 实例化 效率,php类的静态调用和实例化调用有哪些不同点?
- reg型变量怎么赋值_UiPath变量的介绍和使用