JS Learning Essay One: Object simple, deep cloning (copy)

Source: Internet
Author: User

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
  1. var x="1";
  2. var y=x;
  3. Y="2";
  4. "1"
  5. Alert(x);
  6. "2"
  7. Alert(y);
1.2, the value of the cloning
  1. var x=1;
  2. var y=x;
  3. Y=2;
  4. 1
  5. Alert(x);
  6. 2
  7. Alert(y);
1.3. Cloning of Boolean values
  1. var x=true;
  2. var y=x;
  3. Y=false;
  4. True
  5. Alert(x);
  6. False
  7. Alert(y);
2. Cloning of a synthetic type Object 2.1, an array

If a normal clone is used:

  1. var x=[1,2];
  2. var y=x;
  3. Y. Push(3);
  4. The
  5. Alert(x);
  6. The
  7. 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?

  1. var x=[1,2];
  2. var y=[];
  3. var i=0;
  4. var J=x. Length;
  5. For(; I<J; I+ +)
  6. {
  7. Y[i]=x[i];
  8. }
  9. Y. Push(3);
  10. The
  11. Alert(x);
  12. The
  13. 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:

  1. var x={1:2,3:4};
  2. var y={};
  3. var i;
  4. For(i in x)
  5. {
  6. Y[i]=x[i];
  7. }
  8. Y[5]=6;
  9. Object {1:2, 3:4}
  10. Console. Log(x);
  11. Object {1:2, 3:4, 5:6}
  12. 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.

  1. function clone(obj)
  2. {
  3. var o,i,J,K;
  4. if(typeof(obj)! ="Object" | | obj= = =null)return obj;
  5. if(obj instanceof(Array))
  6. {
  7. o=[];
  8. I=0; J=obj. Length;
  9. for(; I<J; I+ +)
  10. {
  11. if(typeof(obj[i]) = = ="Object" && obj[ i]!=null)
  12. {
  13. o[i]=arguments. Callee(obj[i]);
  14. }
  15. Else
  16. {
  17. o[i]=obj[i];
  18. }
  19. }
  20. }
  21. Else
  22. {
  23. o={};
  24. for(i in obj)
  25. {
  26. if(typeof(obj[i]) = = ="Object" && obj[i]! =null)
  27. {
  28. o[i]=arguments. Callee(obj[i]);
  29. }
  30. Else
  31. {
  32. o[i]=obj[i];
  33. }
  34. }
  35. }
  36. return o;
  37. }
4. References
    • Http://javascript.ruanyifeng.com/grammar/basic.html#toc9

JS Learning Essay One: Object simple, deep cloning (copy)

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.