Array.prototype.slice.call(document.querySelectorAll('a'), 0)
Array.prototype.slice.call(document.querySelectorAll('a'), 0)的作用就是将一个DOM NodeList 转换成一个数组。
slice()方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。语法为arr.slice([begin[, end]]),其中arr可为string或Array。更多了解可以参考MDN
document.querySelectorAll('a')获取了一个NodeList对象,它看起来像一个数组,你可以使用方括号来获取其中的节点,你也可以检查它其中包含多少个元素,然后它并没有实现数组包含的所有方法。可以说JavaScript有时是一个古怪的语言,什么是数组的概念也不例外。
这些看似数组又不是真正的伪数组的对象有好几个,其中一个是arguments,arguments是一个特殊的变量,它可以在函数的内部访问到。事实上,arguments列表就是传入的参数列表。这些伪数组没有实现数组包含的所有方法,例如:
var testFun = function({
console.log(arguments);
console.log(arguments.length);
console.log(arguments.shift());
};
testFun(1,2,3)
控制台输入以下信息:
[1, 2, 3]
3
Uncaught TypeError: undefined is not a function
可以看到arguments.shift不是一个函数;而是一个数组函数。在testFun中输入console.log(arguments.constructor),控制台输出"function Object() { [native code] }",而[].constructor将输出"function Array() { [native code] } ".除了arguments,很多DOM(文档对象模型)集合返回的也是这些对象而不是数组:document.getElementsByTagName(),document.all(). 有时我们需要把这些类似于数组但是不是数组的对象变成数组。在这里我们使用了Array.prototype.slice.call。
为什么要这么调用 NodeList的slice 方法呢?就是因为 NodeList不是真的数组,typeof document.querySelectorAll('a')==="Object" 而不是 "Array" 它没有slice这个方法,通过这么Array.prototype.slice.call调用,JS的内部机制把NodeList对象转化为Array。例如:
var my_object = {
'0':'zero',
'1':'one',
'2':'two',
'3':'three',
'4':'four'
};
var my_arr = Array.prototype.slice.call(my_object);
console.log(my_arr.constructor)
输出:function Array() { [native code] } 。可见通过使用Array.prototype.slice.call可以将object转化为Array。
Learning much javascript from one line of code是一篇优秀的文章,这篇文章讲述的是使用[].forEach.call()将NodeList转化为Array。下面是他的代码:
[].forEach.call($$("*"),function(a){
a.style.outline="1px solid #"+(~~(Math.random()*(1<<24))).toString(16)
})
这段代码并不是十分严谨,css的颜色有效值为3位或6位,(~~(Math.random()*(1<<24))).toString(16)并不能保证颜色值是有效的。下面是我在这段代码基础上修改的代码
[].forEach.call($$("*"),function(a){ a.style.outline="1px solid #"+("000000"+(~~(Math.random()*(1<<24))).toString(16)).slice(-6) })
写这篇文章主要目的是巩固下JavaScript基础,整理归纳下,不好的地方,大家拍砖扔鞋吧。
相关文章链接:
Learning much javascript from one line of code
Array-like Objects in JavaScript
Array.prototype.slice.call(document.querySelectorAll('a'), 0)的更多相关文章
- IE下Array.prototype.slice.call(params,0)
i8 不支持 Array.prototype.slice.call(params,0) params可以是 HTMLCollection.类数组.string字符串
- 解析 Array.prototype.slice.call(arguments,0)
Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...
- 对Array.prototype.slice.call()方法的理解在看别人代码时,发现有这么个写法:[].slice.call(arguments, 0),这到底是什么意思呢?
1.基础 1)slice() 方法可从已有的数组中返回选定的元素. start:必需.规定从何处开始选取.如果是负数,那么它规定从数组尾部开始算起的位置.也就是说,-1 指最后一个元素,-2 指倒数第 ...
- js Array.prototype.slice.call(arguments,0) 理解
Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...
- 【javascript 技巧】Array.prototype.slice的妙用
Array.prototype.slice的妙用 开门见山,关于Array 的slice的用法可以参考这里 http://www.w3school.com.cn/js/jsref_slice_arra ...
- JavaScript 兼容 Array.prototype.slice.call
IE9之前的IE版本的HTMLCollection以及NodeList不是Object的子类. 在通过Array.prototype.slice.call进行数组化的时候,IE9之前的IE版本会抛出异 ...
- javascript中 Array.prototype.slice的用法.
首先看到 www.w3school.cn上的解释:http://www.w3school.com.cn/jsref/jsref_slice_array.asp 定义和用法 slice() 方法可从已有 ...
- classlist和array.prototype.slice.call
1.classlist document.getElementById("myDIV").classList.add("mystyle"); classList ...
- Array.prototype.slice.call()的理解
最近在看廖雪峰的JS课程,浏览器中的操作DOM的那一章,有这样一道题. JavaScript Swift HTML ANSI C CSS DirectX <!-- HTML结构 --> & ...
随机推荐
- SqlHelper include Transaction
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- Java BigDecimal大数字操作
在java中提供了大数字的操作类,即java.math.BinInteger类和java.math.BigDecimal类.这两个类用于高精度计算,其中BigInteger类是针对大整数的处理类,而B ...
- <梦断代码>读后感2
<梦断代码>这本书读了一半,我的心情久久不能平静. 为什么好软件如此难做?这是我本人,我想也是很多人都在苦苦思索的一个问题,虽然没有人能有完全确定的答案,但通过书中的记述,和个人思考,还是 ...
- 解释型语言和编译型语言如何交互?以lua和c为例
转自http://my.oschina.net/mayqlzu/blog/113528 问题: 最近lua很火,因为<愤怒的小鸟>使用了lua,ios上有lua解释器?它是怎么嵌入大ios ...
- windows下配置nodejs+npm
windows下安装nodejs是比较方便的 (v0.6.0之后,支持windows native),进入官网http://nodejs.org/ 点击install即可安装.下载完成后一路next ...
- Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树
题目链接: Sum of Medians Time Limit:3000MSMemory Limit:262144KB 问题描述 In one well-known algorithm of find ...
- C++ Template之技巧性基础知识
1.对于T是自定义类型的,如果存在子类型则需要在模版内部加上typename 示例代码: template<typename T> class Myclass { typename T:: ...
- hdu 3926 Hand in Hand 同构图
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3926 In order to get rid of Conan, Kaitou KID disguis ...
- 【CodeForces】【311C】Fetch the Treasures
最短路 神题一道…… //CF 311C #include<queue> #include<cstdio> #include<cstdlib> #include&l ...
- 7 linux服务器程序规范
1. Linux服务器程序一般以后台进程形式运行.后台进程又称守护进程(daemon),它没有控制终端,因而不会意外接收到用户输入.父进程通常为init(PID为1的进程)2. Linux服务器程序常 ...