用法:Array.prototype.slice.call(array-like object)

// 创建一个类数组对象
var alo = {0:"a", 1:"b",2:"c", length:3}; // 转化
var arr = Array.prototype.slice.call(alo); console.log(
Array.isArray(alo) // false
) console.log(
Array.isArray(arr) // true
) console.log(alo); // { '0': 'a', '1': 'b', '2': 'c', length: 3 }
console.log(arr); // [ 'a', 'b', 'c' ]

下面分析它是如何工作的.

类数组对象拥有类似数组的结构,所以它可以使用大多数数组的方法。

数组里有一个方法slice(),遍历调用对象,然后返回一个数组。

这里有一个问题,slice()是数组对象的方法,那该如何使用?

数组方法是数组对象的属性,所以可以通过Array.prototype.slice获取它。

然而在一个对象里调用其它对象的方法,我们需要用到"Function.prototype"里的方法call(),像这样function name.call(obj,args)。

所以有,Array.prototype.slice.call(alo)

slice()是一个函数(function),所以从Function.prototype继承了方法call,这也就是为什么我们可以这样用Array.prototype.slice.call(...)

Function.prototype.call(obj)的第一个参数是一个对象,即将传给f.call(obj)函数里的f,这一步,f的上下文(关键词this)的值会是obj.

一般情况下Array.slice(),方法slice将Array作为this的值,使用索引遍历所有的数组元素。在我们的例子中,我们将alo设置为this的值,alo是类数组,本身又具有索引,因此而工作。

NodeList,HTMLCollection都是类数组对象

<body>
<ul id="ho" class="ho">
<li></li>
</ul>
<script>
// 创建一个htmlCollection对象
var htmlCollection = document.getElementsByTagName("li"); // 创建一个NodeList对象
var nodeList = document.getElementById("ho").childNodes; // 转化
var h2arr = Array.prototype.slice.call(htmlCollection);
var n2arr = Array.prototype.slice.call(nodeList); console.log(htmlCollection); // HTMLCollection [ <li> ]
console.log(nodeList); // NodeList [ #text "", <li>, #text "" ] console.log(h2arr); // Array [ <li> ]
console.log(n2arr); // Array[ #text "", <li>, #text "" ]
</script>
</body>

将类数组对象(array-like object)转化为数组对象(Array object)的更多相关文章

  1. PHP查询数据库,对象结果集转化为数组

    $row = $this->db->get();//得出对象结果集 $result = array(); if($row) { //转化为数组 while($value = $row-&g ...

  2. Javascript中判断变量是 array还是object(是数组还是对象)

    段文字是从github上截取由本人翻译过来的. 原文地址:https://github.com/nathansmith/javascript-quiz/blob/master/ANSWERS.md 怎 ...

  3. Array.from()类数组转化为数组的用法

    类数组对象转化为数组 let arrayLike = { '0': 'a', '1': 'b', '2': 'c', length: 3 }; let arr = Array.from(arrayLi ...

  4. Js将类数组转化为数组

    说起伪数组,大家可能会想到arguments, 这个我们函数参数的一个类数组,是类数组的代表. 1.拥有length属性,可以使用下标来访问元素,这两点和数组相同. 2.不能使用数组的方法,他们不能使 ...

  5. js中怎么把类数组转化为数组

    说起伪数组,首先想到arguments, 这个我们函数参数的一个类数组,是类数组的代表. 1.拥有length属性,可以使用下标来访问元素,这两点和数组相同. 2.不能使用数组的方法,他们不能使用Ar ...

  6. php如何遍历多维的stdClass Object 对象,php的转换成数组的函数只能转换外面一丛数组

    php如何遍历多维的stdClass Object 对象,php的转换成数组的函数只能转换外面一丛数组 (2012-09-10 19:58:49) 标签: 杂谈 分类: 网页基础知识 php如何遍历多 ...

  7. PHP“Cannot use object of type stdClass as array” (php在调用json_decode从字符串对象生成json对象时的报错)

    php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误 错误:Cannot use object of type stdClass as arra ...

  8. php---将数组转化为数组对象

    例子:array(1){ [0]=>array( 'id'=>111, 'name'=>'aaaa' ) } 由上面的例子转化成下面对象,怎么转化?急急 急 谢谢array(1) { ...

  9. Yii框架AR对象数据转化为数组

    demo函数作用:将AR对象数据转化为数组 局限:仅用于findAll的多维数组,find一维数组可以先转化为多维数组的一个元素在使用 function actionIndex() { $data = ...

随机推荐

  1. 经典CSS颜色混合模式

    转自:http://www.webhek.com/css-blend-mode/ 注意:只有使用最新版的谷歌浏览器.火狐浏览器,才能正确的显示本文中的演示. Photoshop里最没有用处的一种功能— ...

  2. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

  3. Monthly Expense(二分) 分类: 二分查找 2015-06-06 00:31 10人阅读 评论(0) 收藏

    Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...

  4. Wikioi 1294 全排列

    先给出链接地址:Wikioi 1294 虽然题目很短,论难度也就是个深搜,算法方面我就不多说了,而且我知道c++有个函数叫next_permutation,谁用谁知道. 代码如下: #include& ...

  5. #include <queue>

    双端队列deque比向量vector更有优势 双端队列(deque) 连续存储的指向不同元素的指针所组成的数组<deque> 队列(queue) 先进先出的值的排列 <queue&g ...

  6. 查看linux/AIX系统内存及CPU占用百分比

    1.linux下查看CPU及内存占用情况 查看内存占用百分比: [root@rusky ~]# free -m | sed -n '2p' | awk '{print "used mem i ...

  7. Apache Thrift入门(安装、测试与java程序编写)

    安装Apache Thrift ubuntu linux运行: #!/bin/bash #下载 wget http://mirrors.cnnic.cn/apache/thrift/0.9.1/thr ...

  8. VirtualBox安装linux增强工具报错

    错误提示: Building the OpenGL support module                         [FAILED] 解决办法 cd /media/VBOXADDITIO ...

  9. PHP zip压缩文件及解压

    PHP zip压缩文件及解压 利用ZipArchive 类实现 只有有函数.界面大家自己写 ZipArchive(PHP 5.3 + 已自带不需要安装dll) /** * 文件解压 * @param ...

  10. WARNING [Project: :app] To shrink resources you must also enable ProGuard

    新版本的Android Gradle plugin中,对于resource有了更加一步的管理,可以把unused resource移除,不仅是自己工程,并且library里面也可以没有用到的,也可以移 ...