Skip to content

Commit c7d34b5

Browse files
committed
[guide] Add a topic about spread operator vs apply
1 parent 030598d commit c7d34b5

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,27 @@ Other Style Guides
764764
}
765765
```
766766

767+
<a name="functions--spread-vs-apply"></a><a name="7.14"></a>
768+
- [7.14](#functions--spread-vs-apply) Prefer the use of the spread operator `...` to call variadic functions. eslint: [`prefer-spread`](http://eslint.org/docs/rules/prefer-spread)
769+
770+
> Why? It's cleaner, you don't need to supply a context, and you can not easily compose `new` with `apply`.
771+
772+
```javascript
773+
// bad
774+
const x = [[1, 2], [3, 4], [5, 6]];
775+
console.log(Array.prototype.concat.apply([], x));
776+
777+
// good
778+
const x = [[1, 2], [3, 4], [5, 6]];
779+
console.log([].concat(...x));
780+
781+
// bad
782+
new (Function.prototype.bind.apply(Date, [null, 2016, 08, 05]));
783+
784+
// good
785+
new Date(...[2016, 08, 05]);
786+
```
787+
767788
**[⬆ back to top](#table-of-contents)**
768789

769790
## Arrow Functions

0 commit comments

Comments
 (0)