js assigns the value of the original object to the new object, and after changing the value of the original object, the value of the new object will also change

Problems encountered at the front end:

When the page is loaded, assign the value returned by the backend to an object 1, then create an object 2, and assign the value of object 1 to object 2, but when the value of object 1 changes, the value of object 2 also changes

//后端返回的data
this.form1=data
this.form2=data
//改变form1的值后  form2的值也会改变
//详细点
//原来  this.form1.name=张三
//现在  this.form1.name=李四
//按照我写的目的来说
//是希望    this.form1.name=李四
//          this.form2.name=张三
//但是当我对form1的name进行改变时
//出现了    this.form1.name=李四
//          this.form2.name=李四
//form2的值也改变了
//通过前端同学的讲解
//这个的原理是  深拷贝和浅拷贝
//大致意思是:  并不是form1和form2的值相等   只是form1和form2的对象的地址相同
//详细可以自行百度
//
//
//出现这种问题的解决办法
//前端同学告诉我  1.可以for循环取出form1的值加入form2
//                2.转换成新对象   我是用下方的方式写的
                this.form2=JSON.parse(JSON.stringify(data));
//                然后form1的值进行改变  form2的值就不会改变了    

Guess you like

Origin blog.csdn.net/weixin_47035997/article/details/127581576