最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - JavaScript: var {left, ...props} = this.props; - Stack Overflow

programmeradmin3浏览0评论

I was reading the source code of Facebook's fixed-data-table, and i found this

var {left, ...props} = this.props;

What that it means? is this a new semantic? I'm confused o.O

I was reading the source code of Facebook's fixed-data-table, and i found this

var {left, ...props} = this.props;

What that it means? is this a new semantic? I'm confused o.O

Share Improve this question asked Feb 16, 2015 at 4:02 Spike886Spike886 6591 gold badge8 silver badges24 bronze badges 4
  • 2 It's ES6 destructuring assignment. – Pointy Commented Feb 16, 2015 at 4:03
  • possible duplicate of Is var { Route, Redirect, RouteHandler, Link } = Router; valid in Javascript? – Qantas 94 Heavy Commented Feb 16, 2015 at 4:13
  • @Qantas94Heavy, It's not quite a duplicate because the other question doesn't mention rest destructuring. – Brigand Commented Feb 16, 2015 at 4:33
  • It's not part of a standard yet and best described in React docs: facebook.github.io/react/docs/transferring-props.html – WiredPrairie Commented Feb 16, 2015 at 11:47
Add a ment  | 

1 Answer 1

Reset to default 16

It's a special form of destructuring assignment proposed for ES7 (and eagerly implemented in the jsx tools and Babel). It creates two variables: left, and props.

left has the value of this.props.left.

props is an object with all of the other properties of this.props (excluding left).

If you wrote it without destructuring it'd look like this:

var left = this.props.left;
var props = {};

Object.keys(this.props).forEach(function(key, index){
    if (key !== 'left') {
        props[key] = this.props[key];
    }
}, this);

That's more than a few characters shaved off :-)

发布评论

评论列表(0)

  1. 暂无评论