1、Array.prototype.includes()

includes()函数用来判断一个数组是否包含一个指定的值,如果包含则返回true,否则返回falseincludes函数与indexOf函数很相似.

下面两个表达式是等价的:

list.includes(x)
// 等价于
list.indexOf(x) >= 0

接下来我们来判断数字中是否包含某个元素,ES7之前:

let arr = ['react', 'angular', 'vue'];
if (arr.indexOf('react') !== -1)
{
    console.log('react存在');
}

ES7使用includes()验证数组中是否存在某个元素:

let arr = ['react', 'angular', 'vue'];
if (arr.includes('react'))
{
    console.log('react存在');
}

2、指数操作符

在ES7中引入了指数运算符****具有与Math.pow(..)等效的计算结果。

使用自定义的递归函数calculateExponent或者Math.pow()进行指数运算:

function calculateExponent(base, exponent)
{
    if (exponent === 1)
    {
        return base;
    }
    else
    {
        return base * calculateExponent(base, exponent - 1);
    }
}
console.log(calculateExponent(2, 10)); // 输出1024
console.log(Math.pow(2, 10)); // 输出1024

使用指数操作符,使用指数运算符**,就像使用+-等操作符一样:

console.log(2**10);// 输出1024

results matching ""

    No results matching ""