1024programmer Java In JavaScript, when an associative array a is assigned to b, and then the content of a is changed, why does b also change?

In JavaScript, when an associative array a is assigned to b, and then the content of a is changed, why does b also change?

JS:
 var a = {"Client":"jQuery","Server":"PHP"};
 var b = a;
 a["New"] = "Element";
 console.log(b);
 // Output Object { Client="jQuery", Server="PHP", New="Element"}

 PHP routine 1:
 $a = array('Client'=>'jQuery','Server'=>'PHP');
 $b = $a;
 $a['New'] = 'Element';
 var_export($b);
 //Output array('Client'=>'jQuery','Server'=>'PHP')

 PHP routine 2:
 $a = array('Client'=>'jQuery','Server'=>'PHP');
 $b = &$a; //Reference assignment
 $a['New'] = 'Element';
 var_export($b);
 //Output array('Client'=>'jQuery','Server'=>'PHP','New'=>'Element')

In Javascript, when an associative array a is assigned to b, and then the content of a is changed, why does b also change?

Reply content:

JS:
 var a = {"Client":"jQuery","Server":"PHP"};
 var b = a;
 a["New"] = "Element";
 console.log(b);
 // Output Object { Client="jQuery", Server="PHP", New="Element"}

 PHP routine 1:
 $a = array('Client'=>'jQuery','Server'=>'PHP');
 $b = $a;
 $a['New'] = 'Element';
 var_export($b);
 //Output array('Client'=>'jQuery','Server'=>'PHP')

 PHP routine 2:
 $a = array('Client'=>'jQuery','Server'=>'PHP');
 $b = &$a; //Reference assignment
 $a['New'] = 'Element';
 var_export($b);
 //Output array('Client'=>'jQuery','Server'=>'PHP','New'=>'Element')

In Javascript, when an associative array a is assigned to b, and then the content of a is changed, why does b also change?

For non-ordinary types such as arrays (strings, integers, Boolean), your assignment is equivalent to address copying, that is, a and b occupy the same address. So if b is changed, a will also change. Essentially, a and b are the same thing.

This answer here is more detailed. Arrays are reference type values ​​and are stored in the heap. https://www.zhihu.com/questio…

I saw someone say on the Internet that when assigning values ​​in JS, original types (such as strings) are copied values, and reference types (such as associative arrays) are copied references.

var a = {"Client":"jQuery","Server":"PHP"};
 var b = JSON.stringify(a); //Convert to string and assign value
 a["New"] = "Element";
 console.log(JSON.parse(b)); //Convert back to associative array (object) when used
 //Output Object { Client="jQuery", Server="PHP"}

 IE8 does not support JSON.parse and JSON.stringify, so json2.js needs to be introduced:
 http://www.json.org/js.html
 https://github.com/douglascrockford/JSON-js/blob/master/json2.js
 Versions below IE9:
 

I feel that JS arrays are not as flexible as PHP. PHP supports & declaration reference assignment, and PHP arrays are “copy-on-write”:

echo round(memory_get_usage()/(1024*1024))."MB\n"; //0MB
 $a = file('/home/eechen/note.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
 echo round(memory_get_usage()/(1024*1024))."MB\n"; //9MB
 $b = $a;
 echo round(memory_get_usage()/(1024*1024))."MB\n"; //9MB (the memory does not change after the assignment)
 $b['new'] = 'element';
 echo round(memory_get_usage()/(1024*1024))."MB\n"; //14MB (the memory changes after modification, that is, copy on write)

Because a and b both point to the same array.

If you want to keep it unchanged, first convert the object into a string, and then convert it back into an object. They will be two different objects. If used directly, it is actually one. The correct answer has been given above.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/in-javascript-when-an-associative-array-a-is-assigned-to-b-and-then-the-content-of-a-is-changed-why-does-b-also-change/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索