简体   繁体   English

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

[英]JavaScript: var {left, …props} = this.props;

I was reading the source code of Facebook's fixed-data-table, and i found this 我正在阅读Facebook的固定数据表的源代码,我发现了这一点

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

What that it means? 这意味着什么? is this a new semantic? 这是一个新的语义? I'm confused oO 我很困惑

It's a special form of destructuring assignment proposed for ES7 (and eagerly implemented in the jsx tools and Babel). 这是为ES7提出的一种特殊形式的解构分配(并且在jsx工具和Babel中热切地实现)。 It creates two variables: left , and props . 它创建了两个变量: leftprops

left has the value of this.props.left . left具有this.props.left的值。

props is an object with all of the other properties of this.props (excluding left ). props是一个具有this.props所有其他属性的this.propsleft除外)。

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 :-) 这超过了几个字符剃掉了:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM