JavaScript Basics-A deep copy of the object type Object

Source: Internet
Author: User
Tags object object

In Javascipt, there are arrays of array objects, object objects, regular objects, function objects, typeof can only be judged as objects, but cannot determine which objects belong to them.

Record a method used to determine what object the object belongs to:

Use Object.prototype.toString.call (elem). toLowerCase () to get the type of the object, and then use the = = to determine whether the object of that type.

See, an object is a value of a reference type, so when assigning an object, you cannot assign a value like a normal type, because after the reference type is assigned, the value of the modified object is directly modified to the value in the reference type, so when an object is assigned, the variable that is assigned to the object is modified if it is later modified.

Therefore, when copying data, a deep copy is required:

Determine if a JSON object is not, 1. The object is not of length, 2. The type is Object 3. Judge is the type of object: Object,objectfunction Isjson (elem) {Console.log (typeof elem);  Console.log (!elem.length);  Console.log (Object.prototype.toString.call (elem) = = = ' [Object Object] '); return typeof Elem = = = "Object" &&!elem.length && Object.prototype.toString.call (elem). toLowerCase () = = "[Object Object]";} Determines whether the array function IsArray (elem) {if (Array.isarray) {return Object.prototype.toString.call (elem). toLowerCase () = = ' [o  Bject array] ';     }else{Console.log (1);  Return Array.isarray (Elem); }}function Issimpletype (elem) {return typeof Elem = = = "string" | | typeof elem = = = ' Number ' | | typeof elem = = = ' Boolean ';}  function Deepcopyjson (JSON) {if (!isjson (JSON)) {return;}  var Newjson = {}; for (Var key in JSON) {if (Isjson (Json[key])) {Newjson[key] = Deepcopyjson (Json[key])}else if (IsArray (Json[key))    ) {Newjson[key] = Deepcopyarray (Json[key])}else{Newjson[key] = Json[key]; }} return Newjson;} function DeepcopyarraY (arr) {if (!isarray (arr)) {return;}  var newArr = [];  Console.log (' Isarr ');      for (Var i=0;i<arr.length;i++) {if (Isjson (Arr[i])) {//Console.log (0);    Newarr[i] = Deepcopyjson (Arr[i]);    }else if (IsArray (Arr[i])) {Newarr[i] = Deepcopyarray (Arr[i]);      }else{console.log (0);    Newarr[i] = Arr[i]; }} return NEWARR;}  function Deepcopy (obj) {var newObj;  if (IsArray (obj)) {newObj = Deepcopyarray (obj);  }else if (Isjson (obj)) {newObj = Deepcopyjson (obj);    } return NEWOBJ;}          var json1 = {' A ': 1, ' B ': ' String ', ' C ': true, ' d ': {' A ': one, ' B ': true, ' C ': {    ' AA ': false, ' BB ': ' Character '}}, ' E ': [1,4,5,{a: ' 555 ', ' B ': ' Hahahah '}]};    var arr1 = [1,4, ' dddd ', {' A ': ' A ', ' B ': ' B '},[11,33,44,55,{' t ': ' t ', ' y ': ' Y '}]; var arr2=deepcopyarray (ARR1);
arr2[4][4].t=3;
Arr1[4][4].1 Output T

  

JavaScript Basics-A deep copy of the object type Object

Related Article

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.