JavaScript 中最新添加的运算符有哪些?
javascriptweb developmentfront end technology
JavaScript 中最新添加的运算符是展开运算符和 rest。
Rest 运算符
使用 rest 参数,您可以将参数数量表示为数组。ES6 引入了 rest 参数以减轻开发人员的工作量。对于参数对象,rest 参数用三个点表示……并位于参数前面。
示例
让我们看以下定义 rest 参数的代码片段
<html>
<body>
<script>
function addition(…numbers) {
var res = 0;
numbers.forEach(function (number) {
res += number;
});
return res;
}
document.write(addition(3));
document.write(addition(9,10,11,12,13));
</script>
</body>
</html>
扩展运算符
它允许表达式扩展到多个参数、元素、变量等。
示例
您可以尝试运行以下代码来了解如何使用扩展运算符
<html>
<body>
<script>
var a, b, c, d, e, f, g;
a = [10,20];
b = "rank";
c = [30, "points"];
d = "run"
// concat method.
e = a.concat(b, c, d);
// spread operator
f = [...a, b, ...c, d];
document.write(e);
document.write("<br>"+f);
</script>
</body>
</html>
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

