Home  >  Article  >  Web Front-end  >  Sample code sharing for JavaScript object reference and assignment

Sample code sharing for JavaScript object reference and assignment

黄舟
黄舟Original
2017-03-17 15:19:17958browse

This article mainly introducesJavaScriptobject reference and assignment, and analyzes the operation skills and related Notes of JavaScript object reference and assignment in the form of examples. , Friends who need it can refer to

The examples in this article describe Javascript object reference and assignment. Share it with everyone for your reference, the details are as follows:

<script type="text/javascript">
//例子一: 引用
var myArrayRef = new Array(0,1,2); //创建数组对象
var mySeconArrayRef = myArrayRef; // 对象复制.
myArrayRef[0] = 100; // 修改元素值
alert(mySeconArrayRef[0]);
/**
* 输出 100; 学过其它语言的都应该知道这里应该输出的是0 为什么输出的是100呢?
* 上面程序通过把myArrayRef对象复制给了mySeconArrayRef这时就存在了2个独立的 但最初值是相同的对象
* 因为是独立的为什么修改myArrayRef会对别一个对象有影响呢?大家都知道只有当他们引用的是同一个对象时这时修改一个才会
* 对别一个产生影响.但是在javascript语言中创建的对象myArrayRef值中其时保存的是对象的引用(也就是一个地址).
* 也就是 我用 new Array生成的保存在内存中而new Array把它所在的地方告诉了myArrayRef,myArrayRef又把这地址告诉了mySeconArrayRef
* 他们两个都指向的是new Array生成对象的地址而不是把对象保存在myArrayRef中,所以通过其中的一个去修改值时其时是修改他们同指象的那对象.
*/
alert(mySeconArrayRef[0] );
//例子二: 赋值
var myVa = &#39;ABC&#39;; //把ABC的值 赋予了myVa
var myVb = myVa; // myVa 赋值给 myVb
myVa = &#39;DEF&#39;; //修改myVa
/**
* 输出的是:ABC. 因为是把值保存在了变量了 而不是保存的是引用地址,所以他们两个是相对独立的整体.
*/
alert(myVb);
</script>

If you really want to copy objects without affecting each other, you must copy the methods and attributes in you by converting assignment or traversing key:value. .

Note: The sub-objects of objects are also references, so when traversing assignments, it is necessary to determine whether the sub-elements are objects. If the sub-elements are objects, continue traversing and assigning values ​​to the sub-elements.

Conversion assignment method:

var data = {a:1,b:2,c:3,d:[0,1,2,3]};
var str = JSON.stringify(data);
var data1 = $.parseJSON(str); //$为jQuery对象需要引入jQuery包
data1["e"] = 4;
data1["d"][0] = 11;
console.log(data);
console.log(data1);

Output result:

{a: 1, b: 2, c: 3, d: [0,1,2,3]}
{a: 1, b: 2, c: 3, d: [11,1,2,3], e: 4}

No influence on each other

When object references are passed as function parameters , they will still affect each other. Remember , as in the following example:

var data = {a:1,b:2,c:3,d:{q:4,w:5,e:6}};
var data1 = data;
function con(data2){
data2["r"] = 5;
console.log(JSON.stringify(data2));
}
con(data1);
console.log(JSON.stringify(data));

Output result:

{"a":1,"b":2,"c":3,"d":{"q":4,"w":5,"e":6},"r":5}
{"a":1,"b":2,"c":3,"d":{"q":4,"w":5,"e":6},"r":5}

After the object reference is assigned, if the object is left blank, they will not be affected , as follows:

var arr = {"a":"1","b":"2"};
var arr1 = arr;
arr = {};
arr["a"] = 2;
console.log(arr1);
console.log(arr);

Output result:

{"a":"1","b":"2"},{"a":2}

The above is the detailed content of Sample code sharing for JavaScript object reference and assignment. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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