问题
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!
回答1:
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.
来源:https://stackoverflow.com/questions/49957500/what-is-3-dots-in-javascript