什么是javascript中的"......"(3个点)?

Nic*_*.Xu 3 ecmascript-6 reactjs

我从这篇文章中了解到了这一点.

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;
}
Run Code Online (Sandbox Code Playgroud)

请用示例代码解释什么是"...".谢谢!

acd*_*ior 12

在该示例中,...Rest参数,语法允许我们将无限数量的参数表示为数组.

它有点类似(或不是:),但它与扩展语法不同.


在您的示例中,stores内部的参数是一个数组.如果function StoreMixin(...stores)被称为StoreMixin(1,2,3)那么stores将是[1, 2, 3]等等.