Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "..." (3 dots) in javascript?

I learned about this thing from this post.

function StoreMixin(...stores) { // what is "..."
  var Mixin = {
    getInitialState() {
      return this.getStateFromStores(this.props);
    },
    componentDidMount() {
      stores.forEach(store =>
        store.addChangeListener(this.handleStoresChanged)
      );
      this.setState(this.getStateFromStores(this.props));
    },
    componentWillUnmount() {
      stores.forEach(store =>
        store.removeChangeListener(this.handleStoresChanged)
      );
    },
    handleStoresChanged() {
      if (this.isMounted()) {
        this.setState(this.getStateFromStores(this.props));
      }
    }
  };
  return Mixin;
}

Please kindly explain what is "...", with example code. Thanks!

like image 607
Nicolas S.Xu Avatar asked Apr 21 '18 15:04

Nicolas S.Xu


People also ask

What do 3 dots mean in Typescript?

The three dots are known as the spread operator from Typescript (also from ES7). The spread operator return all elements of an array.

What does ellipsis do in JavaScript?

In Javascript, ellipses ( … ) are used for two separate shorthands — rest syntax and spread syntax. Rest syntax will set the remaining elements of a collection to a defined variable.

What is the dot operator in JavaScript?

The dot (.) is simply an operator that sits between its operands, just like + and -. By convention, the variables stored in an object that we access via the dot operator are generically called properties. Properties that happen to be functions are called methods.

What is $() in JS?

The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document. getElementById("id_of_element").


1 Answers

In that example, the ... is a Rest parameter, a syntax allows us to represent an indefinite number of arguments as an array.

It is somewhat similar (or not :), but it's not the same as the spread syntax.

In your example, the stores argument inside is an array. If function StoreMixin(...stores) is called like StoreMixin(1,2,3) then stores will be [1, 2, 3] and so on.

like image 75
acdcjunior Avatar answered Nov 15 '22 04:11

acdcjunior