ES7 - 新函数

ES7 - 新函数 首页 / ES6入门教程 / ES7 - 新函数

本章提供有关ES7中新函数的知识。

求幂运算符

ES7引入了一种新的数学运算符,称为幂运算符。该运算符类似于使用Math.pow()方法。幂运算符由双星号**表示。该运算符只能与数值一起使用。下面给出了使用幂运算符的语法-

base_value ** exponent_value

下面的示例使用 Math.pow()方法和指数运算符来计算数字的指数。

<script>
   let base = 2
   let exponent = 3
   console.log('using Math.pow()',Math.pow(base,exponent))
   console.log('using exponentiation operator',base**exponent)
</script>

上面这段代码的输出如下:

using Math.pow() 8
using exponentiation operator 8

数组包括

ES7中引入的Array.includes()方法有助于检查数组中的元素是否可用。在ES7之前,可以使用Array类的indexof()方法来验证array中是否存在值。如果找到数据,则indexof()返回数组中第一次出现的元素的索引;如果不存在数据,则返回-1。

链接:https://www.learnfk.comhttps://www.learnfk.com/es6/es7-newfeatures.html

来源:LearnFk无涯教程网

Array.includes()方法接受一个参数,检查作为参数传递的值在数组中是否存在。如果找到该值,则此方法返回true;否则,如果该值不存在,则返回false。下面给出了使用Array.includes()方法的语法-

无涯教程网

Array.includes(value)

要么

Array.includes(value,start_index)

第二种语法检查指定索引中是否存在该值。

示例

下面的示例声明一个数组标记,并使用Array.includes()方法来验证数组中是否存在值。

<script>
   let marks = [50,60,70,80]
   //检查 50 是否包含在数组中
   if(marks.includes(50)){
      console.log('found element in array')
   }else{
      console.log('could not find element')
   }

   // 检查是否从索引 1 中找到 50
   if(marks.includes(50,1)){ //从索引 1 搜索
      console.log('found element in array')
   }else{
      console.log('could not find element')
   }

   //检查不是数组中的数字(NaN)
   console.log([NaN].includes(NaN))

   //创建对象数组
   let user1 = {name:'kannan'},
   user2 = {name:'varun'},
   user3={name:'prijin'}
   let users = [user1,user2]

   //检查对象在数组中可用
   console.log(users.includes(user1))
   console.log(users.includes(user3))
</script>

上面代码的输出将如下所示-

found element in array
could not find element
true
true
false

ES6目录

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

左耳听风 -〔陈皓〕

玩转Spring全家桶 -〔丁雪丰〕

系统性能调优必知必会 -〔陶辉〕

编译原理实战课 -〔宫文学〕

说透数字化转型 -〔付晓岩〕

大数据经典论文解读 -〔徐文浩〕

搞定音频技术 -〔冯建元 〕

Kubernetes入门实战课 -〔罗剑锋〕

云原生架构与GitOps实战 -〔王炜〕

好记忆不如烂笔头。留下您的足迹吧 :)