es7 --- 新特性

ES7只有2个特性:

不使用ES7

使用indexOf()验证数组中是否存在某个元素,这时需要根据返回值是否为-1来判断:

 
                                                    
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存在');
}
 
 

指数操作符

不使用ES7

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

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

使用ES7

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

 
                                                    
console.log(7**3);
 
 

 

posted @ 2018-10-30 18:03  蔚京霖  阅读(284)  评论(0编辑  收藏  举报