All instances of JavaScript are objects, except that the objects are slightly different, divided into primitive types and composite types. The primitive type object refers to a string (string), a numeric value (number), a Boolean (Boolean), a composite type object that refers to an array (array), an object, or a function.
Since the objects are divided into these two categories, the biggest difference between them is the difference between replication clones. Ordinary objects store the actual data of the object, while the reference object stores the object's reference address, and the actual content of the object is stored separately, because the reference object is usually quite large, which is the means of data overhead and memory overhead optimization. It is often difficult for beginners to understand this part of the content, just like the prototype of the object, is also the same concept. The prototype of the object is also a reference object, which puts the methods and properties of the prototype in separate memory, while the object's prototype chain points to the memory address. Although this part of the content is more difficult, the principle is consistent, and the purpose is consistent.
1. Clone of primitive type Object 1.1, String clone
- var x="1";
- var y=x;
- Y="2";
- "1"
- Alert(x);
- "2"
- Alert(y);
1.2, the value of the cloning
- var x=1;
- var y=x;
- Y=2;
- 1
- Alert(x);
- 2
- Alert(y);
1.3. Cloning of Boolean values
- var x=true;
- var y=x;
- Y=false;
- True
- Alert(x);
- False
- Alert(y);
2. Cloning of a synthetic type Object 2.1, an array
If a normal clone is used:
- var x=[1,2];
- var y=x;
- Y. Push(3);
- The
- Alert(x);
- The
- Alert(y);
From the above, the original array x, the clone array y, modified the clone array y, but also modified the original array x, which is the characteristics of the Reference object. So how do you get a complete array clone?
- var x=[1,2];
- var y=[];
- var i=0;
- var J=x. Length;
- For(; I<J; I+ +)
- {
- Y[i]=x[i];
- }
- Y. Push(3);
- The
- Alert(x);
- The
- Alert(y);
In this way, the clone array y, the original array x, two array complementary interference, implements the complete array clone.
2.2. Cloning of objects
And the cloning of an array, the complete cloning of the object is as follows:
- var x={1:2,3:4};
- var y={};
- var i;
- For(i in x)
- {
- Y[i]=x[i];
- }
- Y[5]=6;
- Object {1:2, 3:4}
- Console. Log(x);
- Object {1:2, 3:4, 5:6}
- Console. Log(y);
2.3. Cloning of functions
var x=function () {alert (1);}; var y=x;y=function () {alert (2);};/ /function () {alert (1);}; alert (x);//Y=function () {alert (2);}; alert (y);
The cloning of the function, using the "=" symbol, and changing the cloned object, will not affect the object before cloning, because the cloned object will be copied once and store the actual data, is the real clone.
3. Complete object cloning
Based on 1 and 2, summarize the complete object clones, including cloning ordinary objects and referencing objects. Before writing this method, we must think that the cloned reference object must take a full clone (deep clone), including the object's value is also an object to complete cloning (deep cloning).
Complete object cloning is also called deep object cloning, deep cloning of objects, deep copying of objects, and so on.
- function clone(obj)
- {
- var o,i,J,K;
- if(typeof(obj)! ="Object" | | obj= = =null)return obj;
- if(obj instanceof(Array))
- {
- o=[];
- I=0; J=obj. Length;
- for(; I<J; I+ +)
- {
- if(typeof(obj[i]) = = ="Object" && obj[ i]!=null)
- {
- o[i]=arguments. Callee(obj[i]);
- }
- Else
- {
- o[i]=obj[i];
- }
- }
- }
- Else
- {
- o={};
- for(i in obj)
- {
- if(typeof(obj[i]) = = ="Object" && obj[i]! =null)
- {
- o[i]=arguments. Callee(obj[i]);
- }
- Else
- {
- o[i]=obj[i];
- }
- }
- }
- return o;
- }
4. References
- Http://javascript.ruanyifeng.com/grammar/basic.html#toc9
JS Learning Essay One: Object simple, deep cloning (copy)