对象类型的变量A,将A赋值给变量B,将A的某属性赋值给变量C,更改变量C的值,B也会自动跟着被更改?

小程序 文章 2022-03-11 10:00 442 0 全屏看文

AI助手支持GPT4.0

对象类型的变量A,将A赋值给变量B,将A的某属性赋值给变量C,更改变量C的值,B也会自动跟着被更改?Object type variable A, assign A to variable B, assign an attribute of A to variable C, change the value of variable C, and B will be automatically changed?

对象类型的变量A,将A赋值给变量B,将A的某属性赋值给变量C,更改变量C的值,B也会自动跟着被更改?

有如下代码:

    var id = that.data.wid

    var tfl = that.data.tfl   

    var tpnew = that.data.tfl.tp

    var qa = that.data.queryResult

    var zts = qa[id].zts  

    var tpold = '' 

    if(zts&&zts.length>0){

      zts.forEach(function(item){

        if(item.zt==that.data.title&&item.hasOwnProperty('tp')){

          tpold=item.tp

        }

      })

    }

    if(tpold){

      if(tpnew.length>0){

        tpold.forEach(function(item){

          var okg = 0 

          tpnew.forEach(function(it,index){

            if(item==it){

              console.log('tfl:',tfl)

              console.log('tpnew:',tpnew)

              tpnew.splice(index,1) ///////////修改tpnew的值

              console.log('tpnew:',tpnew)

              okg=1

              console.log('tfl2:',tfl) ////////这时候显示tfl的值也被改了

            }

            if(index==tpnew.length-1&&okg==0){

              console.log('id:',that.data.swid)

            }

          })

        })

      }

    }

在运行过程中,更改了tpnewr的值后,tfl的值也随之改变,请问我的代码出错在哪?

有如下代码: var id = that.data.wid var tfl = that.data.tfl var tpnew = that.data.tfl.tp var qa = that.data.queryResult var zts = qa[id].zts var tpold = '' if(zts&&zts.length>0){ zts.forEach(function(item){ if(item.zt==that.data.title&&item.hasOwnProperty('tp')){ tpold=item.tp } }) } if(tpold){ if(tpnew.length>0){ tpold.forEach(function(item){ var okg = 0 tpnew.forEach(function(it,index){ if(item==it){ console.log('tfl:',tfl) console.log('tpnew:',tpnew) tpnew.splice(index,1) ///////////修改tpnew的值 console.log('tpnew:',tpnew) okg=1 console.log('tfl2:',tfl) ////////这时候显示tfl的值也被改了 } if(index==tpnew.length-1&&okg==0){ console.log('id:',that.data.swid) } }) }) } } 在运行过程中,更改了tpnewr的值后,tfl的值也随之改变,请问我的代码出错在哪?

回答:

小黎:

去了解下对象深拷贝和浅拷贝

总得通:谢谢您,己解决了。
老张:

js基本知识了。

一般自己写几行代码演示一下就明白了。

总得通:师傅,可否明示一二?我刚学的,弄了两天都不明白。
老张:类似这样的代码,你写几句就明白了。对象变量是指针引用,或者你百度一下“JS对象深拷贝”。
总得通:谢谢!
总得通:

-EOF-

AI助手支持GPT4.0