正文
源自组内一个文件上传组件,从某个版本开始,同学A觉得代码里面有太多没被引用的变量就把它全删了,但是他没发布,后来同学B基于这个代码发布了一个新包,结果,bug出现了。
原始代码:
function render() {
const {className, onChange, ...others} = this.props;
return <Uploader className={className} onChange={this.onUploadChange} {...others}/>;
}
删除无用变量onChange
之后:
function render() {
const {className, ...others} = this.props;
return <Uploader className={className} onChange={this.onUploadChange} {...others}/>;
}
可以看到,删除onChange
后others
里面多了一个onChange
,结果这个被透传的others
中的onChange
无意中把有业务代码的this.onUploadChange
给覆盖了,最终导致了致命bug的出现,由于这个bug很隐蔽,上线很久后才被发现,但是已经被多个地方引用,只能将错就错下去。
总结
不要以为删除没有引用的变量就一定安全,要慎重啊!