亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

javascript - js array map method, if the original array contains objects or arrays, the original array will also change. Why?
淡淡煙草味
淡淡煙草味 2017-05-19 10:42:07
0
2
815
var list = [{'a': 1},{'a': 2}];
var newList = list.map(function(index){
    return index.a += 1;
});
console.log(newList,'newList',list,'list');
// list也改變了 list = [{'a': 2},{'a': 3}]
// 本人小白,求大神指教,勿噴,謝謝!
淡淡煙草味
淡淡煙草味

reply all(2)
PHPzhong

It has nothing to do with map

js objects are reference types, characters and numbers are basic types

Basic type value transfer is copying

Reference type passing by value is a reference

For example:

var a = 1;
var b = a;
b++;
console.log(a);

and

var a = [1];
var b = a;
b[0]++;
console.log(a);
我想大聲告訴你

You first modify a single key value of the list, and then return the key value, naturally modifying two!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template