JavaScript assigns an associative array A to B, and then changes the contents of a, why does B change as well?

Source: Internet
Author: User
JS:var a = {"Client":"jQuery","Server":"PHP"};var b = a;a["New"] = "Element";console.log(b);// 输出 Object { Client="jQuery",  Server="PHP",  New="Element"}PHP例程1:$a = array('Client'=>'jQuery','Server'=>'PHP');$b = $a;$a['New'] = 'Element';var_export($b);//输出 array('Client'=>'jQuery','Server'=>'PHP')PHP例程2:$a = array('Client'=>'jQuery','Server'=>'PHP');$b = &$a; //引用赋值$a['New'] = 'Element';var_export($b);//输出 array('Client'=>'jQuery','Server'=>'PHP','New'=>'Element')

JavaScript assigns an associative array A to B, and then changes the contents of a, why does B change as well?

Reply content:

JS:var a = {"Client":"jQuery","Server":"PHP"};var b = a;a["New"] = "Element";console.log(b);// 输出 Object { Client="jQuery",  Server="PHP",  New="Element"}PHP例程1:$a = array('Client'=>'jQuery','Server'=>'PHP');$b = $a;$a['New'] = 'Element';var_export($b);//输出 array('Client'=>'jQuery','Server'=>'PHP')PHP例程2:$a = array('Client'=>'jQuery','Server'=>'PHP');$b = &$a; //引用赋值$a['New'] = 'Element';var_export($b);//输出 array('Client'=>'jQuery','Server'=>'PHP','New'=>'Element')

JavaScript assigns an associative array A to B, and then changes the contents of a, why does B change as well?

For an array of such non-trivial types (String, Integer, Boolean), your assignment is equivalent to the copy of the address, that is, a, B occupies the same address. So change the b,a will also change, in essence A, B is a thing.

This answer is detailed here, the array belongs to the reference type value, stored in the heap. Https://www.zhihu.com/questio ...

Online to see some people say that when the assignment in JS, the original type (such as String) is a copy of the value, reference type (such as associative array) is a copy reference.

var a = {"Client":"jQuery","Server":"PHP"};var b = JSON.stringify(a); //转成字符串后赋值a["New"] = "Element";console.log(JSON.parse(b)); //使用时转回关联数组(对象)//输出 Object { Client="jQuery",  Server="PHP"}IE8不支持JSON.parse和JSON.stringify,需要引入json2.js:http://www.json.org/js.htmlhttps://github.com/douglascrockford/JSON-js/blob/master/json2.jsIE9以下版本:
  
   

The sense JS array is not PHP flexible, PHP support with & Declaration reference assignment, PHP array is "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(赋值后内存没有变化)$b['new'] = 'element';echo round(memory_get_usage()/(1024*1024))."MB\n"; //14MB(修改后内存发生变化,即写时复制)

Because A and B point to the same number of groups AH.

If you want to do not change, the object is converted to a string, and then converted back to the object, is two different objects. Directly with the words, is actually a. The positive solution has been given upstairs.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.