Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

求大佬来解释一下这个react问题

image.png

this.props.detailInfo是个对象,对象里面有name和decription字段,
为什么我已经赋值到新的变量里了,我改变这个新的变,为什么this.props.detailInfo也会跟着改变!

求大神解释一下!非常迷惑
本人QQ885150804


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

数组,对象,类,他们的赋值都是地址引用,你把props的值复制给detailInfo相当于将detailInfo的值的地址设置为props.detailInfo的内存地址,改变其中的一个值,另一个也会跟着改变,详细的你可以看看深拷贝和浅拷贝,不论哪个语言都可以,原理是一样的。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...