ES6 Spread Operator

October 27, 2015

One of my favorite feature of ES6 is the spread operator that is represented by .... It is comparable in Ruby to the splat operator *.

I use this feature all the time in my code, it really simplify the code readability by removing repetition.

You can use it to merge array:

// javascript
const previousArray = [3, 4];
const array = [1, 2, ...previousArray]; // => [1, 2, 3, 4];
# ruby
array = [1, 2]
newArray = [*array, 3, 4] # => [1, 2, 3, 4];

Call a function that can get an unknown number of arguments:

// javascript
function foo(...args) {
  console.log(args.length) // => 3
}

foo('a', 'b', 'c');

# ruby
def foo(*args)
  puts args.size # => 3
end

foo('a', 'b', 'c');

Here is an example of refactoring by using the spread operator for the Timeshift library:

Here is a link to the commit.

Comments

comments powered by Disqus