Javascript中的类数组对象(Array-like object)指的是一些看起来像数组但又不是数组的对象。Javascript中的arguments变量、document.getElementsByTagName()返回值就是典型的类数组对象。

类数组特性

  • 类数组对象具有一个length属性且length的值为非负整数。
  • 类数组对象不具有数组对象的方法。例如:push、 pop等方法。

类数组对象可以像数组一样遍历,但不支持数组对象的方法。

  1. function test1() {
  2. for(var i = 0; i < arguments.length; i++) {
  3. console.log(arguments[i]);
  4. }
  5.  
  6. console.log(arguments instanceof Array);
  7. }
  8. test1(2, 3);
  9. /* 输出
  10. 2
  11. 3
  12. false
  13. */
  14.  
  15. function test2(){
  16. arguments.push(1);
  17. }
  18. test2();
  19. /* 报错
  20. TypeError: undefined is not a function
  21. */

将类数组对象转化为数组

slice 方法可以用来将一个类数组(Array-like)对象转换成一个数组。 你只需要用数组原型上的slice方法call这个对象。

  1. function arrayify( a ) {
  2. return Array.prototype.slice.call( a );
  3. }
  4.  
  5. function test3() {
  6. var arrayified = arrayify(arguments);
  7. arrayified.push(1);
  8. console.log(arrayified);
  9. console.log(arrayified instanceof Array);
  10. }
  11.  
  12. test3(2, 3);
  13. /* 输出
  14. [2, 3, 1]
  15. true
  16. */

也可以简单的使用 [].slice.call(a)代替Array.prototype.slice.call(a)

slice 方法可以用来将一个类数组(Array-like)对象/集合转换成一个数组。你只需将该方法绑定到这个对象上。下述代码中 list 函数中的 arguments 就是一个类数组对象。

  1. function list() {
  2. return Array.prototype.slice.call(arguments);
  3. }
  4.  
  5. var list1 = list(1, 2, 3); // [1, 2, 3]

除了使用 Array.prototype.slice.call(arguments),你也可以简单的使用[].slice.call(arguments) 来代替。另外,你可以使用 bind 来简化该过程。

  1. var unboundSlice = Array.prototype.slice;
  2. var slice = Function.prototype.call.bind(unboundSlice);
  3.  
  4. function list() {
  5. return slice(arguments);
  6. }
  7.  
  8. var list1 = list(1, 2, 3); // [1, 2, 3]

实例代码

  1. Array.prototype.slice.apply(document.querySelectorAll('.total-con li')).map((item)=>{
  2. if(item.className == 'hover1') {
  3. this.sortField = item.getAttribute('sortField')
  4. this.sortType = item.getAttribute('sortType')
  5. return
  6. }
  7. })

Javascript 类数组(Array-like)对象的更多相关文章

  1. javascript 类数组对象

    原文:https://segmentfault.com/a/1190000000415572 定义: 拥有length属性,其他属性(索引)为非负整数(对象中的所有会被当做字符串来处理,这里你可以当做 ...

  2. javascript类数组

    一.类数组定义: 而对于一个普通的对象来说,如果它的所有property名均为正整数,同时也有相应的length属性,那么虽然该对象并不是由Array构造函数所创建的,它依然呈现出数组的行为,在这种情 ...

  3. JavaScript中数组Array方法详解

    ECMAScript 3在Array.prototype中定义了一些很有用的操作数组的函数,这意味着这些函数作为任何数组的方法都是可用的. 1.Array.join()方法 Array.join()方 ...

  4. JavaScript类数组转换为数组 面试题

    1.JavaScript类数组转换为数组 (1)方法一:借用slice (2)方法二:Array.from 2.代码 <!DOCTYPE html> <html lang=" ...

  5. JavaScript类数组对象参考

    JavaScript和DOM中有很多类数组对象,它们有以下特点 1.有length属性 2.可以使用[]通过下标访问 3.部分类数组对象使用[]访问成员时不只可以使用下标,还可以使用id或name 4 ...

  6. javascript类型系统——数组array

    × 目录 [1]创建 [2]本质 [3]稀疏[4]长度[5]遍历[6]类数组 前面的话 除了对象之外,数组Array类型可能是javascript中最常用的类型了.而且,javascript中的数组与 ...

  7. javascript中数组Array的方法

    一.常用方法(push,pop,unshift,shift,join)push pop栈方法,后进先出var a =[1,2,3];console.log(a.push(40)); //4 返回数组的 ...

  8. JavaScript中数组Array.sort()排序方法详解

    JavaScript中数组的sort()方法主要用于对数组的元素进行排序.其中,sort()方法有一个可选参数.但是,此参数必须是函数. 数组在调用sort()方法时,如果没有传参将按字母顺序(字符编 ...

  9. NodeJS对象数组Array 根据对象object key的值排序sort

    有个js对象数组 var ary=[{id:1,name:”b”},{id:2,name:”b”}] 需求是根据name 或者 id的值来排序,这里有个风骚的函数. /** * 对数组中的对象,按对象 ...

随机推荐

  1. [转载]函数getopt(),及其参数optind

    最近用到了getopt()这个函数,对它进行了一些了解.这篇博文还是写的非常清楚的.值得学习.最近在改进一个开源项目,希望自己能静下心好好分析代码. ------------------------- ...

  2. UVA 11796 Dog Distance(向量)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=31962 [代码] #include<cstdio> # ...

  3. [JIT_APP]Activity生命周期相关的7个方法

    先发一张安卓官方文档里面的Activity生命周期图解 下面在对这7个生命周期内相关的方法做一些简单的介绍 OnCreate() 当Activity被创建的时候,会自动运行该方法.该方法做一些初始化动 ...

  4. 《University Calculus》-chape8-无穷序列和无穷级数-基本极限恒等式

    基于基本的极限分析方法(诸多的无穷小以及洛必达法则),我们能够得到推导出一些表面上看不是那么显然的式子,这些极限恒等式往往会在其他的推导过程中用到,其中一个例子就是概率论中的极限定理那部分知识.

  5. hdu 4293 dp求最大权值不重合区间

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4293 #include<cstdio> #include<cstring> # ...

  6. ArrStack——数组栈(procedure)

    //数组栈,对于无法预料栈的长度情况下,可能会因为原分配数组不够长而导致数据溢出,或因为数组太长而浪费空间.但是操作快,不需要额外的操作.而链表与此想法,可以动态分配内存,但是要增加额外的操作. #i ...

  7. dispatch_get_current_queue 废弃

    由于iOS7以后 dispatch_get_current_queue 被废弃,所以需要寻找一个替代的方案. 发现 dispatch_get_current_queue 并没有字面上那么简单. 这个函 ...

  8. Android教程:ImageView 设置图片

    Android doc中是这样描述的: public void setImageResource (int resId) 这是其中的一个方法,参数resld是这样: ImageView.setImag ...

  9. Java基础知识强化之集合框架笔记33:Arrays工具类中asList()方法的使用

    1. Arrays工具类中asList()方法的使用 public static <T> List<T> asList(T... a): 把数组转成集合 注意事项: 虽然可以把 ...

  10. iOS UIKit:App

    1.App生命周期 IOS架构是由许多设计模式实现,如model-view-controller 和 delegation模式. 1.1 main函数 与其它框架类似,IOS框架的入口也是从main函 ...