使用AngularJs,代碼片段的較大程序的部分:X = {}創建自發對象
var thePortList = $scope.ipPortList; // an array of objects
...
...
console.log("before", $scope.ipPortList, thePortList);
thePortList[i] = {};
console.log("after", $scope.ipPortList, thePortList);
創建此輸出:
before [] []
after [2: Object] [2: Object]
我不知道爲什麼。我可以期望在多線程程序中看到類似的內容,但不能用Javascript。
x = {}應該爲數組中的元素指定一個新的空對象。對?我對這種行爲感到困惑。任何人都可以點亮一下嗎?
在我的本地計算機和plunkr上的Chrome中進行了測試。
Plunkr這裏:
完全plunkr控制檯輸出:Plunker Link
Event tracked Multipane Show Preview Toolbar undefined undefined
editor-0.11.1.js:2 URL visited /?p=preview
VM631:29 portList []
VM631:34 processing 0 [] undefined
VM631:39 skipping 0
VM631:34 processing 1 [] undefined
VM631:39 skipping 1
VM631:34 processing 2 [] undefined
VM631:45 before [] []
VM631:47 after [2: Object] [2: Object]
您的預期產出是什麼?對象出現在'$ scope.ipPortList'和'thePortList'的* both *中是否存在問題?因爲這是正確的行爲,因爲它們都指向相同的數組。 – nnnnnn
你能告訴我們你期望的行爲嗎 –