4 ms·
I like the functionality of let merged = { ...foo, ...bar, ...baz }; But I've come to understand ... as variadic parameters in C++, Java and Go. Wish the
by libria 10y ago
I like the functionality of
let merged = { ...foo, ...bar, ...baz };
But I've come to understand ... as variadic parameters in C++, Java and Go. Wish they'd used another token.
- patates 10y agoIt depends on the context. In JS variadic parameters are also supported. This is like complaining they confusingly use curly braces around function bodies while you got used to them surrounding object declarations :)
- tomatsu 10y agoSpread (for arrays) was added with ES6. This just adds that kind of thing for objects (ES2017). let a = [1, 2, 3]; Math.max(...a); // 3 If you use that operator in parameters, it's rest (i.e. variadic stuff). function sum(...numbers) { return numbers.reduce((a, b) => a + b, 0); } sum(1, 2, 3, 4, 5); // 15
- WorldMaker 10y agoJS admits that ... is two operators ("spread" and "rest"), but that they are essential related/cousins (dual in mathematics terminology) and it works well for both of them to look the same. Rest is the variadic case: function myFunction(a, b, ...rest) { } But rest can also be used for destructuring, such as: let [a, b, ...rest] = ['a', 'b', 'c', 'd'] Or let { a, b, ...rest } = { a: 'a', b: 'b', c: 'c', d: 'd' } In this case it's a extra-bang-for-the-buck expansion of variadic parameters into something more generally useful: tearing apart lists (and now objects). From that light, spread is the "structuring" side of rest, in the left-hand/right-hand world of destructuring/structuring.