This is the 23rd day of my participation in the August More Text Challenge

1. Data types

Data is divided into basic data types (String, Number, Boolean, Null, Undefined) and reference data types

  • Basic data type features: Stores the actual data of the object

  • Object data type features: Store the object reference in the stack, the real data is stored in the heap memory

2. Replicate data

Basic data type replication is ok:

     

But there is a problem when you copy objects/arrays

Why change the value of b and a after copying array?!! ?

Why did you change the value of b and a after copying the object?!! ?

Let me explain why this is happening

The variables declared by this code are stored in memory like this.

When you copy with the equal sign, the primitive data type copies a value directly, but the reference data type just creates a new pointer to your original data.

This is a shallow copy

ShallowCopy: simply adds a pointer to the memory address of the copied object.

 

So what is deep copy?

DeepCopy: adds a pointer to a new memory and makes the added pointer point to the new memory.

 

Of course, one caveat: whether you use shallow copy or deep copy, you can delete one variable without affecting the other.

3. Common copy technologies

3.1 Concat () shallow copy of array

Grammar: array1. Concat (array2, array3,… ,arrayX)

Parameters: array2, array3… ArrayX, which can be a concrete value or an array object. It can be as many as you want.

The Array object returns a new Array.

3.2 Shallow copy of slice() arrays

Syntax: array.slice(start, end)

Parameters:

  • Start the optional. Specify where to start. If it is negative, it specifies the position from the end of the array. That is, -1 is the last element, -2 is the next-to-last element, and so on

  • The end is optional. Specify where to end the selection. This parameter is the array subscript at the end of the array fragment. If this parameter is not specified, the shard array contains all elements from start to the end of the array. If this parameter is negative, it specifies the element from the end of the array.

 

Returned value: Array Returns a new Array containing elements from the arrayObject from start to end (excluding the element).

As in the above case, it is a shallow copy.

3.3… Rest expanded array/shallow copy of objects

Make a shallow copy of the array

Shallow copy of the object

3.4 Json.parse (json.stringify ()) deep copy of array/object, but cannot process function data

Test object deep copy successful!

Test array deep copy also successful!!

But it becomes null when it encounters a function. Because there are only objects and arrays in JSON, there are no functions.

 


4. Customize the deep-copy function

So what? Can you write one yourself? : No_mouth: I’ll have to write one myself, really. The code:

code

                     var a = [1."22"[1.1.1].function() {}]

                     var b = copy(a)

                     // b[2] = 100000

                     console.log(a, b)

 

                     // Get the data type,

                     function CheckType(variable) {

                            return Object.prototype.toString.call(variable).slice(8, -1)}// Implement deep copy

                     function copy(v) {

                            let result

                            let type = CheckType(v)

 

                            // Initialize result

                            if (type === "Object") {

                                   result = {}

                            } else if (type === "Array") {

                                   result = []

                            } else {

                                   result = v

                            }

 

                            // Iterate over the target data

                            for (let i in v) {

                                   // Get each item of target data

                                   let value = v[i]

                                   // Check whether each item is an array or an object

                                   if (CheckType(value) === 'Object' || CheckType(value) === 'Array') {

                                          // Handle nested content recursively

                                          result[i] = copy(value)

                                   } else {

                                          result[i] = value

                                   }

                            }

                            return result

                     }

Copy the code

 

parsing

Object. The prototype. ToString. Call (), call this method, the default returns the current Object of type string, the format for [Object] Xxx, Xxx which is the type of the Object. The type is then segmented using the slice() method of strings.

Six ways JavaScript can determine data types

 

test

Let’s test it. It worked. Ow


Good night ~