From c7d34b526a8343c8688c5a68412bcf33445465b0 Mon Sep 17 00:00:00 2001 From: felipethome Date: Fri, 5 Aug 2016 11:52:41 -0300 Subject: [PATCH] [guide] Add a topic about spread operator vs apply --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 909cbbf0..cc312913 100644 --- a/README.md +++ b/README.md @@ -764,6 +764,27 @@ Other Style Guides } ``` + + - [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) + + > Why? It's cleaner, you don't need to supply a context, and you can not easily compose `new` with `apply`. + + ```javascript + // bad + const x = [[1, 2], [3, 4], [5, 6]]; + console.log(Array.prototype.concat.apply([], x)); + + // good + const x = [[1, 2], [3, 4], [5, 6]]; + console.log([].concat(...x)); + + // bad + new (Function.prototype.bind.apply(Date, [null, 2016, 08, 05])); + + // good + new Date(...[2016, 08, 05]); + ``` + **[⬆ back to top](#table-of-contents)** ## Arrow Functions