js 中连续的 3 个点 three dots (...) in javascript
Posted hangj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js 中连续的 3 个点 three dots (...) in javascript相关的知识,希望对你有一定的参考价值。
https://dev.to/sagar/three-dots---in-javascript-26ci
5 种用法
1
function myFunc(...[x, y, z])
return x * y* z;
myFunc(1) // NaN
myFunc(1, 2, 3) // 6
myFunc(1, 2, 3, 4) // 6 (fourth parameter is not destructured)
2
function myFunc(x, y, ...params) // used rest operator here
console.log(x);
console.log(y);
console.log(params);
var inputs = ["a", "b", "c", "d", "e", "f"];
myFunc(...inputs); // used spread operator here
// "a"
// "b"
// ["c", "d", "e", "f"]
3
const featured = ['Deep Dish', 'Pepperoni', 'Hawaiian'];
const specialty = ['Meatzza', 'Spicy Mama', 'Margherita'];
const pizzas = [...featured, 'veg pizza', ...specialty];
console.log(pizzas); // 'Deep Dish', 'Pepperoni', 'Hawaiian', 'veg pizza', 'Meatzza', 'Spicy Mama', 'Margherita'
4
var obj1 = foo: 'bar', x: 42 ;
var obj2 = foo: 'baz', y: 13 ;
var clonedObj = ...obj1 ;
// Object foo: "bar", x: 42
var mergedObj = ...obj1, ...obj2 ;
// Object foo: "baz", x: 42, y: 13
5
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(rest); // [30,40,50]
let a, b, ...rest = a: 10, b: 20, c: 30, d: 40
console.log(a); // 10
console.log(b); // 20
console.log(rest); // c: 30, d: 40
以上是关于js 中连续的 3 个点 three dots (...) in javascript的主要内容,如果未能解决你的问题,请参考以下文章