本章提供有关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。
Array.includes()方法接受一个参数,检查作为参数传递的值在数组中是否存在。如果找到该值,则此方法返回true;否则,如果该值不存在,则返回false。下面给出了使用Array.includes()方法的语法-
Array.includes(value)
要么
链接:https://www.learnfk.comhttps://www.learnfk.com/es6/es7-newfeatures.html
来源:LearnFk无涯教程网
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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
PromptX3人工智能提示词案例库 -〔仰望星空TAN星星〕
Random Thoughts Generator -〔Scarlett〕