In JavaScript, an associated array a is assigned to B, and then the content of a is changed. Why does B change?

Source: Internet
Author: User
{Code...} assigns an associated array a to B in JavaScript and changes the content of a. Why does B 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 ['new'] = 'element'; var_export ($ B); // output array ('client' => 'jquery ', 'server' => 'php') PHP routine 2: $ a = array ('client' => 'jquery ', 'server' => 'php '); $ B = & $ a; // value assigned to the reference $ a ['new'] = 'element'; var_export ($ B ); // output array ('client' => 'jquery ', 'server' => 'php', 'new' => 'element ')

In JavaScript, an associated array a is assigned to B, and then the content of a is changed. Why does B 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 ['new'] = 'element'; var_export ($ B); // output array ('client' => 'jquery ', 'server' => 'php') PHP routine 2: $ a = array ('client' => 'jquery ', 'server' => 'php '); $ B = & $ a; // value assigned to the reference $ a ['new'] = 'element'; var_export ($ B ); // output array ('client' => 'jquery ', 'server' => 'php', 'new' => 'element ')

In JavaScript, an associated array a is assigned to B, and then the content of a is changed. Why does B change?

For an array of a non-normal type (string, integer, or Boolean), your assignment is equivalent to an address copy, that is, a and B occupy the same address segment. So when B is changed, a will change. essentially, a and B are a thing.

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

It is said on the Internet that when values are assigned in JS, the original type (such as string) is the copy value, and the reference type (such as the associated array) is the copy reference.

Var a = {"Client": "jQuery", "Server": "PHP"}; var B = JSON. stringify (a); // Convert it to a string and assign a value to a ["New"] = "Element"; console. log (JSON. parse (B); // returns the associated array (Object) when used // output Object {Client = "jQuery", Server = "PHP"} IE8 does not support JSON. parse and JSON. stringify, json2.js: require needs to be introduced:
  

I feel that JS arrays are not flexible in PHP. PHP supports reference assignment with & declaration, and PHP arrays are "copy at write ":

Echo round (memory_get_usage ()/(1024*1024 )). "MB \ n"; // 0 MB $ a = file ('/home/eechen/note.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); echo round (memory_get_usage () /(1024*1024 )). "MB \ n"; // 9 MB $ B = $ a; echo round (memory_get_usage ()/(1024*1024 )). "MB \ n"; // 9 MB (the memory remains unchanged after the value is assigned) $ B ['new'] = 'element'; echo round (memory_get_usage () /(1024*1024 )). "MB \ n"; // 14 MB (after modification, the memory changes, that is, copy at write time)

Because both a and B point to the same array.

To do this without changing, first convert the object into a string and then the object is two different objects. If it is used directly, it is actually one. A positive solution has been provided upstairs.

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.