Javascript 初学者问题:这种破坏模式如何比早期的方法更好?
我有这个:Javascript 初学者问题:这种破坏模式如何比早期的方法更好?,javascript,reactjs,eslint,destructuring,Javascript,Reactjs,Eslint,Destructuring,我有这个: componentWillReceiveProps(newProps) { if (this.props.width !== newProps.width) { setTimeout(this._onResize, 0); } } eslint警告说我可以将其更改为: componentWillReceiveProps({ width }) { const { width2 } = this.props; if (width2 !==
componentWillReceiveProps(newProps) {
if (this.props.width !== newProps.width) {
setTimeout(this._onResize, 0);
}
}
eslint警告说我可以将其更改为:
componentWillReceiveProps({ width }) {
const { width2 } = this.props;
if (width2 !== width) {
setTimeout(this._onResize, 0);
}
}
警告消失了,但这样做是否正确,因为它的代码更多???道具是否包含
width2
?如果没有,那么width2
将始终是未定义的(这两个代码片段并不相等)@JaromandaX谢谢你的权利,嗯。我想我必须更改更高的变量名,也许你的意思是const{width:width2}=this.props代码>解构只是分配来自同一对象的值的一种更简单的方法,我想说它可能更具品味、代码指南和可读性。解构参数还清楚地向任何使用者表明,消费者实际将使用哪些属性。无透视代码并不一定意味着更好。