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}] // 本人小白,求大神指教,勿喷,谢谢!
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!
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:
and
You first modify a single key value of the list, and then return the key value, naturally modifying two!