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基础,整理归纳下,不好的地方,大家拍砖扔鞋吧。

满屏的彩虹球(demo)

相关文章链接:

Learning much javascript from one line of code

Array-like Objects in JavaScript

Array.prototype.slice.call(document.querySelectorAll('a'), 0)的更多相关文章

  1. IE下Array.prototype.slice.call(params,0)

    i8 不支持 Array.prototype.slice.call(params,0) params可以是 HTMLCollection.类数组.string字符串

  2. 解析 Array.prototype.slice.call(arguments,0)

    Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...

  3. 对Array.prototype.slice.call()方法的理解在看别人代码时,发现有这么个写法:[].slice.call(arguments, 0),这到底是什么意思呢?

    1.基础 1)slice() 方法可从已有的数组中返回选定的元素. start:必需.规定从何处开始选取.如果是负数,那么它规定从数组尾部开始算起的位置.也就是说,-1 指最后一个元素,-2 指倒数第 ...

  4. js Array.prototype.slice.call(arguments,0) 理解

    Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...

  5. 【javascript 技巧】Array.prototype.slice的妙用

    Array.prototype.slice的妙用 开门见山,关于Array 的slice的用法可以参考这里 http://www.w3school.com.cn/js/jsref_slice_arra ...

  6. JavaScript 兼容 Array.prototype.slice.call

    IE9之前的IE版本的HTMLCollection以及NodeList不是Object的子类. 在通过Array.prototype.slice.call进行数组化的时候,IE9之前的IE版本会抛出异 ...

  7. javascript中 Array.prototype.slice的用法.

    首先看到 www.w3school.cn上的解释:http://www.w3school.com.cn/jsref/jsref_slice_array.asp 定义和用法 slice() 方法可从已有 ...

  8. classlist和array.prototype.slice.call

    1.classlist document.getElementById("myDIV").classList.add("mystyle"); classList ...

  9. Array.prototype.slice.call()的理解

    最近在看廖雪峰的JS课程,浏览器中的操作DOM的那一章,有这样一道题. JavaScript Swift HTML ANSI C CSS DirectX <!-- HTML结构 --> & ...

随机推荐

  1. chmod修改文件权限的命令

    语法: chmod [options] mode files options: -c,--changes只输出被改变文件的信息-f,--silent,--quiet当chmod不能改变文件模式时,不通 ...

  2. 在数据表中添加一个字段的SQL语句怎么写

    如果要在数据表中添加一个字段,应该如何表示呢?下面就为您介绍表添加字段的SQL语句的写法,希望可以让您对SQL语句有更深的认识.   通用式: alter table [表名] add [字段名] 字 ...

  3. 多线程 -- GCD

    GCD中有2个核心概念 任务:执行什么操作 队列:用来存放任务 执行任务 同步方法: dispatch_sync dispatch_sync(dispatch_queue_t queue, dispa ...

  4. Ant学习---第五节:Ant_Junit介绍(基于3的版本)

    Junit3 和 Junit4 有本质上的区别 1.普通java类,代码如下: package learn.junit; public class HelloWorld { public String ...

  5. 20145103《java程序设计》第五周学习总结

    20145103<Java程序设计>第5周学习总结 教材学习内容总结 第八章 异常处理 1.设计错误对象都继承自java.lang.Throwable类 2.Java中所有错误都会被打包为 ...

  6. Notes of the scrum meeting(11/2)

    meeting time:13:00~13:30p.m.,November 2nd,2013 meeting place:3号公寓楼一层 attendees: 顾育豪                  ...

  7. 【转】eclipse技巧1

    俗话说的好啊,“工于利启事,必先善其器”,如果说你的编程功底是一个枪法的话,那么强大的eclipse就是android战士们最好的武器. 这里,我们来总结eclipse的使用技巧,从而使我们的编程达到 ...

  8. 学习Linux第三天

    1.常用的命令: reset 清屏 leave +hhmm 建立离开提醒 sudo apt-get yum 安装yum程序 sudo su 切换root身份 see test.c 可以直接查看文件,神 ...

  9. WPS for ubuntu14

    QGtkStyle could not resolve GTK. Make sure you have installed the proper libraries. sudo apt-get ins ...

  10. SqlServer Split函数

    Create FUNCTION [dbo].[SplitToTable] ( @SplitString nvarchar(max), @Separator nvarchar(10)=' ' ) RET ...