`
jjfat
  • 浏览: 265521 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ES7:只是做了一点微小的工作

 
阅读更多

本篇文章将会为大家介绍一下JavaScript最新版本:ECMAScript 2016(就是大家通常说的ES7)。ES7说,我的改动不多不多,也就两个小特征:Array.prototype.includes(),以及幂运算符(exponential operator): **

 

Array.prototype.includes()

 

我们需要用.indexOf()来判断一个元素是否存在于数组的日子一去不复返啦。

 

['my','mom','hates','me'].indexOf('mom')  // 1
// I know it sounds confusing but the value 1 represents 
// the index at which the string 'mom' appears in the array.
// Not that I only have 1 mom.

 

请注意,关键字是''exist.''

 

如果我们想知道给定元素在数组中的索引值,用.indexOf()完全OK。

 

但是如果我们的目的是知道给定元素是否存在于数组中,很显然.indexOf()并不是一个很好的选项。理由很简单:但我们查询一个东西是否存在时,我们希望的到的结果是一个布尔值,而不是一个数字。

 

Array.prototype.includes()就可以做到。它可以确定一个给定元素是否存在于一个数组中,如果存在返回true,否则返回flase。

 

var life = ['mom', 'dad', 'brother']
life.includes('mom')          // true
life.includes('girlfriend')   // false

 

规范

 

Array.prototype.includes ( searchElement [ , fromIndex ] )
  • serchElement-要寻找的元素
  • fromIndex(可选) - 开始搜索位置的索引值

查看规范就像是在寻找力量

 

规范都说了些啥?

 

不要着急,让我们一步一步来,然后用例子来理解这些规范。


# includes
# [1] searchs in ascending order
> Array(10000000).concat(4).includes(4)
true # [Time] 500 miliseconds
> [4].concat(Array(10000000)).includes(4)
true # [Time] 90 miliseconds

# [2] uses SameValueZero algorithm
> [NaN].indexOf(NaN)
-1
> [NaN].includes(NaN)
true

# [3] if found at any position returns true
# otherwise, false is returned
> [1, 2, 3].includes(2)
true
> [1, 2, 3].includes(7)
false

# [4] it treats missing array elements as undefined
> [1, , 3].indexOf(undefined)
-1

1.这里的区别是元素'4'的位置。在我们的第一个例子中我们将4放在后面的位置,includes将会搜索整个数组。据详细说明,.includes() 寻找到searchElement之后会立即返回。于是第二个操作更快。

 

2.SameValueZero算法与Strict Equality Comparison(.indexOf()使用的)相比较,它居然允许寻找NAN元素!

 

3.当元素被找到时时返回true,否则返回false,再也不是返回索引值啦!

 

4.与.indexOf()不同的是,.includes()没有忽略数组中的缺失元素。

 

你感受到了这股神秘的力量了吗?

 

现在我们再来讲fromIndex

 

让我们来看看这个规范:

这个可选的第二个参 fromIndex默认值是0;如果大于或等于数组的长度,将会返回false,数组将不会被搜索; 如果是负数,将会从数组尾部开始偏移来计算fromIndex(array length + fromIndex),如果计算出来的index小于0,整个数组将会被搜索一遍。

# fromIndex
# [1] it defaults to 0
> [1,2,3].includes(1)
true
# [2] if >= array.length, false is returned
> [1,2,3].includes(1, 10)
false
# [3] if negative, it is used as the offset, i.e.
# offset = array.length + fromIndex
> [1,2,3].includes(3, -2) # fromIndex = 3 (array length) + -2 (fromIndex) = 1
true
# [4] if offset < 0, the whole array is searched
> [1,2,3].includes(1, -5) # fromIndex = 0

 

1.如果没有给fromeIndex值,将会使用默认值0,整个数组将会被搜索一遍。

 

2.如果fromIndex比数组的长度还大,.includes() 将会立即返回false

 

3.当 fromIndex 为负数时,将会得到一个计算值 array.length + fromIndex作为它的值。当你要寻找的元素在数组尾部时这将非常有用。例如, fromIndex = -5 将等同于搜索最后5个元素。

 

4.为了避免.includes()方法崩溃, 当fromIndex的值小于0时,整个数组都将被搜索一遍。

 

好啦,要说到最后一个特性了。。。


幂运算符( **)

 

我们终于可以像加、减、乘、除一样进行幂运算啦

 

**操作符跟Math.pow()的用法一样,将第一个操作数作为底,第二个擦作数为幂进行运算(比如 x ** y)。

# x ** y (aka Math.pow(x,y))
> 2 ** 2
4
> 2 ** 'operand'
NaN

 

好啦,今天的介绍到此结束,ES7的能力已传送完毕,赶紧用起来吧~

 

via medium.freecodecamp.org

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics