當(dāng)前位置:
首頁(yè) >
JS:js中的复制对象值问题——Object.assign()
發(fā)布時(shí)間:2025/3/15
26
豆豆
生活随笔
收集整理的這篇文章主要介紹了
JS:js中的复制对象值问题——Object.assign()
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在復(fù)制對(duì)象的值的時(shí)候,往往不能直接“=”,這樣會(huì)造成引用賦值,應(yīng)該利用一些函數(shù)進(jìn)行對(duì)象的復(fù)制值。如下:
$scope.updateDeliveryOrder = function(wayPointsOrder) {var tempDeListInfo = Object.assign({}, $scope.deListInfo);var index = 1 ;for( var i = 0; i < wayPointsOrder.length; i++ ){$scope.deListInfo[i] = tempDeListInfo[wayPointsOrder[i]];$scope.deListInfo[i].order = $scope.beginOrder + index;$scope.deListInfo[i].indexImg = "images/seq/seq_"+index+".png";$scope.makerLocation($scope.deListInfo[i], index);index ++;}$scope.$apply();//強(qiáng)制更新數(shù)據(jù)}Object.assign()?方法用于將所有可枚舉屬性的值從一個(gè)或多個(gè)源對(duì)象復(fù)制到目標(biāo)對(duì)象。它將返回目標(biāo)對(duì)象。
const target = { a: 1, b: 2 }; const source = { b: 4, c: 5 };const returnedTarget = Object.assign(target, source);console.log(target); // expected output: Object { a: 1, b: 4, c: 5 }console.log(returnedTarget); // expected output: Object { a: 1, b: 4, c: 5 }?
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的JS:js中的复制对象值问题——Object.assign()的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SpringCloud Ribbon实战
- 下一篇: SpringAOP的CGLIB动态代理的