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!
The three dots are known as the spread operator from Typescript (also from ES7). The spread operator return all elements of an array.
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.
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.
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").
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With