Javascript中三点运算符的含义是什么?

时间:2015-10-13 01:19:44

标签: javascript reactjs

我看到rubix代码

http://wrapbootstrap.com/preview/WB09498FH(网站点击演示点击)

它是反应组件中的代码

javascript

//react ES6
var InboxItem = React.createClass({
  mixins: [State, Navigation],
  statics: {
    ID: 0,
    resetID: function() {
      InboxItem.ID = 0;
    },
    getID: function() {
      return ++InboxItem.ID;
    }
  },
  handleClick: function(e) {
    e.preventDefault();
    e.stopPropagation();
    this.transitionTo('/app/mailbox/mail');
  },
  render: function() {
    var classes = classNames({
      'inbox-item': true,
      'unread': this.props.unread
    });

    var props = {
      href: '/app/mailbox/mail',
      onClick: this.handleClick,
      ...this.props,
      className: classes
    };

    return (
      <a {...props}>
        <div className='inbox-avatar'>
          <img src={this.props.src} width='40' height='40' className={this.props.imgClass + ' hidden-xs'} />
          <div className='inbox-avatar-name'>
            <div className='fg-darkgrayishblue75'>{this.props.name}</div>
            <div><small><Badge className={this.props.labelClass} style={{marginRight: 5, display: this.props.labelValue ? 'inline':'none'}}>{this.props.labelValue}</Badge><span>{this.props.description}</span></small></div>
          </div>
          <div className='inbox-date hidden-sm hidden-xs fg-darkgray40 text-right'>
            <div style={{position: 'relative', top: 5}}>{this.props.date}</div>
            <div style={{position: 'relative', top: -5}}><small>#{InboxItem.getID()}</small></div>
          </div>
        </div>
      </a>
    );
  }
});

点击下一行代码

... this.props

返回下一行代码

a {... props}

这是什么&#34; ...&#34; ?

1 个答案:

答案 0 :(得分:13)

正如comments中提到的@zerkms;它是ECMAScript 2016 (ES7) Proposal中的对象休息/传播属性运算符,也在React docs中提到。

您看到的代码

var props = {
  href: '/app/mailbox/mail',
  onClick: this.handleClick,

  ...this.props,

  className: classes
};

得到以下评估,其中props对象属性扩展到新的props对象:

var props = {
  href: '/app/mailbox/mail',
  onClick: this.handleClick,

  src: 'https://example.com',
  name: 'myName',
  labelClass: 'myLabelClass',
  labelValue: 'mylabelValue',

  className: classes
};