當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JS:js中的复制对象值问题——Object.assign()
生活随笔
收集整理的這篇文章主要介紹了
JS:js中的复制对象值问题——Object.assign()
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在復制對象的值的時候,往往不能直接“=”,這樣會造成引用賦值,應該利用一些函數進行對象的復制值。如下:
$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();//強制更新數據}Object.assign()?方法用于將所有可枚舉屬性的值從一個或多個源對象復制到目標對象。它將返回目標對象。
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位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的JS:js中的复制对象值问题——Object.assign()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringCloud Ribbon实战
- 下一篇: Mysql: SQL JOIN 子句详解