再探JS数组原生方法—没想到你是这样的数组
最近作死又去做了一遍javascript-puzzlers上的44道变态题,这些题号称“JS语言专业八级”的水准,建议可以去试试,这里我不去解析这44道题了,网上已经有很多的答案了。我只介绍让我意想不到的几种特殊情况下的数组操作方法结果。关于数组原生方法的基本操作我在另一篇博客里已经做了简介:吃透Javascript数组操作的正确姿势—再读《Js高程》.....下面的输出结果,未做特殊说明是在Node环境中运行的结果。
第一题:
What is the result of this expression? (or multiple ones)
[ [3,2,1].reduce(Math.pow), [].reduce(Math.pow) ]
an error[9, 0][9, NaN][9, undefined]
Per spec: reduce on an empty array without an initial value throws TypeError
在Node环境中可以自行执行上面的表达式,结果会产生TypeError: Reduce of empty array with no initial value
也就是说在不能在空数组上调用不带初始参数的reduce方法,可以像下面这样调用,增加一个初始参数
|
1
|
console.log([ [3,2,1].reduce(Math.pow), [].reduce(Math.pow,1)])//输出[9,1] |
第二题:
What is the result of this expression? (or multiple ones)
var ary = [0,1,2];ary[10] = 10;ary.filter(function(x) { return x === undefined;});
[undefined × 7][0, 1, 2, 10][][undefined]
Array.prototype.filter is not invoked for the missing elements.
由上面的结果可以得知,“稀疏数组”中丢失的元素并不会唤起filter方法的调用,但是如果你打印出稀疏元素
|
1
|
console.log(ary[5]);//undefined |
结果确实是undefined。打印整个数组看一下:
|
1
|
console.log(ary)//[ 0, 1, 2, , , undefined, , , , , 10 ] |
所以这种通过直接给ary[10]赋值的方式产生的数组并不将未赋值的地方变成真正的undefined。
创建“稀疏数组”的方法也不只这一种,那么其他方法会不会也是这种情况呢。
|
1
2
3
4
|
var ary = [0,1,2];ary.length = 10;console.log(ary)//[ 0, 1, 2, , , , , , , ]console.log(ary.filter(function(x) { return x === undefined;})); //[] |
从上例可以看到使用设置arr.length的方法创建的稀疏数组也是一样的情况。
|
1
2
3
4
5
6
|
var ary = Array(10);ary[0]=0;ary[1]=1;ary[2]=2;console.log(ary)//[ 0, 1, 2, , , , , , , ]console.log(ary.filter(function(x) { return x === undefined;})); //[] |
从上可以看出通过ary = Array(10);创建的稀疏数组同样出现这样情况。
接下来拓展一下,直接将undefined赋值给ary[5]
|
1
2
3
4
5
|
var ary = [0,1,2];ary[5]=undefined;//直接将undefined赋值给ary[5]ary[10] = 10;console.log(ary)//[ 0, 1, 2, , , undefined, , , , , 10 ]console.log(ary.filter(function(x) { return x === undefined;})); //[ undefined ] |
从结果中可以看出只有ary[5]通过了筛选。
稀疏数组的这种特性在数组的map方法中是否存在呢,接着看第三题。
第三题:
What is the result of this expression? (or multiple ones)
var ary = Array(3);ary[0]=2;ary.map(function(elem) { return '1'; });
[2, 1, 1]["1", "1", "1"][2, "1", "1"]other The result is ["1", undefined × 2], as map is only invoked for elements of the Array which have been initialized.
官网给出了上面的 解析,在浏览器中运行就是上面的结果,我在Node环境中显示的结果是[ '1', , ]。不管怎么样都说明了在稀疏元素上并没有唤起调用map的回调函数。
2016-7-20补充:那么有没有办法让稀疏数组唤起调用map或者上一题的filter方法呢?看完下面的例子大概就清楚了
|
1
2
3
4
5
|
var a = Array(10).join(",").split(",").map(function(item, index) { return index;});console.log(a); |
通过join方法把它转成字符串,然后,再通过split方法把字符串转成数组,这时候,它就拥有10个值为空的元素了,然后,再通过map函数,改变这些元素的值即可。

第四题:
What is the result of this expression? (or multiple ones)
var x = [].reverse;x();
[].reverse will return this and when invoked without an explicit receiver object it will default to the default this AKA window
reverse 方法颠倒数组中元素的位置,并返回该数组的引用。出题者的意图是该方法会返回this,然后在全局环境中这个this就是window,而实际上经过我测试,在谷歌浏览器上显示错误:
Uncaught TypeError: Array.prototype.reverse called on null or undefined(…)
在Node环境中显示TypeError: Array.prototype.reverse called on null or undefined
可以看出实际上 var x = [].reverse返回的是对数组reverse方法的引用,直接调用的话会出错。可以使用call方法调用
|
1
2
3
4
|
var x = [].reverse;var arr=[1,2,3];console.log( x.call(arr)); //[ 3, 2, 1 ] |
第五题:
What is the result of this expression? (or multiple ones)
[,,,].join(", ")
JavaScript allows a trailing comma when defining arrays, so that turns out to be an array of three undefined.
因为javascript 在定义数组的时候允许最后一个元素后跟一个,, 所以这是个长度为三的稀疏数组(这是长度为三, 并没有 0, 1, 2三个属性哦),同理
|
1
2
|
var arr=Array(3);console.log(arr.join(", ") )//, , |
参考:
44个 Javascript 变态题解析 (上)
再探JS数组原生方法—没想到你是这样的数组的更多相关文章
- JS 中 原生方法 (二) --- 数组 (修---添加ES6新增)
const arr = [1, 2, 3, 5, 'a', 'b'] /** * * length * 这个只能被 称之为 数组的原生属性, 返回 一个 number * arr.length */ ...
- JS 中 原生方法 (四) --- Object
Javascript 中 str. arr.date.obj 等常见的原生方法总结 本文也说主要阐释了 Javascript 中的基础类型和 引用类型的自带方法,那么熟悉的同学又可以绕道了 总是绕道, ...
- JS 中 原生方法 (三) --- Date 日期
本文也说主要阐释了 Javascript 中的基础类型和 引用类型的自带方法,那么熟悉的同学又可以绕道了 总是绕道,真是羞耻悳boy 当然 本文阐述的主要类容 from MDN ( zh-cn ) D ...
- JS 中 原生方法 (一) --- 字符串
目录 Javascript 中 str. arr.date.obj 等常见的原生方法总结 Javascript 中 str. arr.date.obj 等常见的原生方法总结 本文也说主要阐释了 Jav ...
- N皇后求解。万万没想到,只用一个一维数组就搞定了。还体现了回溯。
一.啥是N皇后?先从四皇后入手 给定一个4x4的棋盘,要在棋盘上放置4个皇后.他们的位置有这样的要求,每一列,每一行,每一对角线都能有一个皇后. 你可能会对这个对角线有疑惑,其实就是每一个小正方形的对 ...
- 浅谈Vue响应式(数组变异方法)
很多初使用Vue的同学会发现,在改变数组的值的时候,值确实是改变了,但是视图却无动于衷,果然是因为数组太高冷了吗? 查看官方文档才发现,不是女神太高冷,而是你没用对方法. 看来想让女神自己动,关键得用 ...
- 原生Js 两种方法实现页面关键字高亮显示
原生Js 两种方法实现页面关键字高亮显示 上网看了看别人写的,不是兼容问题就是代码繁琐,自己琢磨了一下用两种方法都可以实现,各有利弊. 方法一 依靠正则表达式修改 1.获取obj的html2.统一替换 ...
- 原生JS中apply()方法的一个值得注意的用法
今天在学习vue.js的render时,遇到需要重复构造多个同类型对象的问题,在这里发现原生JS中apply()方法的一个特殊的用法: var ary = Array.apply(null, { &q ...
- 再谈React.js实现原生js拖拽效果
前几天写的那个拖拽,自己留下的疑问...这次在热心博友的提示下又修正了一些小小的bug,也加了拖拽的边缘检测部分...就再聊聊拖拽吧 一.不要直接操作dom元素 react中使用了虚拟dom的概念,目 ...
随机推荐
- linux 查找文件或目录
find / -maxdepth 2 -name "vmware*"在根目录/ 2层深度下搜索以vmware打头的文件或者目录
- 基于ASP.NET MVC(C#)和Quartz.Net组件实现的定时执行任务调度
http://www.cnblogs.com/bobositlife/p/aspnet-mvc-csharp-quartz-net-timer-task-scheduler.html 在之前的文章&l ...
- php内部函数
strpos函数 /** haystack:被比较字串首地址(指向被比较字符串) needle:源字串首地址(指向源字符串) needle_len:源字符串长度 end:指向最后一个字符地址的下一个内 ...
- 再谈如何使用Taglist?
以前使用个taglist, 但是只是查看, 没有熟练的去使用, 补充一下: 参考文章: http://blog.csdn.net/vaqeteart/article/details/4146618 用 ...
- Lattice Reduction (LLL) 算法C代码实现
废话不多说,大名鼎鼎的Lenstra-Lenstra-Lovasz(LLL) 算法.实现参考论文:Factoring Polynomials with Rational Coefficients, 作 ...
- 【转】C# 将字符串或表达式直接转为可执行代码的办法
C# 将字符串或表达式直接转为可执行代码的办法 反射类 using System; using System.Data; using System.Configuration; using Syste ...
- Adaboost\GBDT\GBRT\组合算法
Adaboost\GBDT\GBRT\组合算法(龙心尘老师上课笔记) 一.Bagging (并行bootstrap)& Boosting(串行) 随机森林实际上是bagging的思路,而GBD ...
- 计算机维修之操作系统的安装(windows、Mac、Linux)
从大学开始接触了更多的计算机知识,我就在开荒一样,慢慢的接触多了,就想着安装操作系统一定要学会. 前前后后呢,我折腾过很多操作系统,在我的笔记本上存在过Windows.黑苹果.Linux.安卓等操作系 ...
- [转]python 常用类库!
Python学习 On this page... (hide) 1. 基本安装 2. Python文档 2.1 推荐资源站点 2.2 其他参考资料 2.3 代码示例 3. 常用工具 3.1 Pytho ...
- 最新Mac OS X 10.12.1 安装cocoapods及使用详解
cocoapods官网:https://cocoapods.org 一.什么是CocoaPods 每种语言发展到一个阶段,就会出现相应的依赖管理工具,例如 Java 语言的 Maven,nodejs ...