7

I have a problem with code I am supposed to work with. I found a syntax I am not familiar with and I have trouble googling the documentation:

export const Something = class Something {
    constructor(someObject = {}) {
        this.someObject = {...Something.someObjectDefaultAsStaticMethod,...someThing};
    };
// The rest of the class
};

I have problems understanding what the three dots (...) in front of the parameter do. And "dots in parameter javascript" is a bad search term. Can someone help me, maybe tell me what this syntax is actually called or just directly link me to documentation?

str
  • 33,096
  • 11
  • 88
  • 115
SomeStranger314
  • 167
  • 1
  • 10
  • 2
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator – Keith Jan 26 '18 at 15:18
  • 1
    Possible duplicate of [Spread Syntax ES6](https://stackoverflow.com/questions/34559918/spread-syntax-es6) – BlackBeard Aug 03 '18 at 10:35
  • Does this answer your question? [Spread Syntax vs Rest Parameter in ES2015 / ES6](https://stackoverflow.com/questions/33898512/spread-syntax-vs-rest-parameter-in-es2015-es6) – Henke Mar 16 '21 at 08:06

3 Answers3

9

That is not ES6 but has only been added in ECMAScript 2018.

It is called "Object Rest/Spread Properties" and is part of the Spread Syntax.

str
  • 33,096
  • 11
  • 88
  • 115
  • 4
    This answer is correct. But just to clarify - the array rest / spread operator is in ES6, the _object_ is in Stage 3. – dayuloli Jan 26 '18 at 15:22
4

... (three dots in Javascript) is called the Spread Syntax or Spread Operator. This allows an iterable such as an array expression or string to be expanded or an object expression to be expanded wherever placed.

I want to list down the mostly used practical Use Cases of the Spread Syntax (Spread Operator). The following has been explained with examples in this stackoverflow answer.

  1. Combine Arrays (Concatenate Arrays)
  2. Copying Arrays
  3. Calling Functions without Apply
  4. Destructuring Arrays
  5. Function Arguments as Rest Parameters
  6. Using Math Functions
  7. Combining Two Objects
  8. Separate a String into Separate Characters
Keet Sugathadasa
  • 4,497
  • 1
  • 37
  • 53
3

The [...something] is the spread operator. It in essence allows for an array or string to be expanded. You will see it used often in React, but has many other use cases.

MDN has great documentation on the spread operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator

bdurb
  • 31
  • 3