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

WBOY
Release: 2016-08-23 09:17:45
Original
1906 people have browsed it

<code>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')</code>
Copy after login
Copy after login

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:

<code>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')</code>
Copy after login
Copy after login

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 on the Internet saying that when assigning values ​​in JS, original types (such as strings) are copied values, and reference types (such as associative arrays) are copied references.

<code>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.html
https://github.com/douglascrockford/JSON-js/blob/master/json2.js
IE9以下版本:
<!--[if lt IE 9]><script src="json2.js"></script><![endif]--></code>
Copy after login

I feel that JS arrays are not as flexible as PHP. PHP supports reference assignment using & declarations. PHP arrays are "copy-on-write":

<code>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(修改后内存发生变化,即写时复制)</code>
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!