In-depth exploration of the front-end: analysis and implementation of JavaScript deep copy and shallow copy

introduction

In front-end development, data copying is a common operation. Especially when dealing with objects and arrays, we need to consider whether it is a shallow copy or a deep copy. So, what are deep copy and shallow copy? What role do they play in front-end development? How to achieve these two copies? This is the question we will discuss in this article.

Shallow copy

Shallow copy is a data copy method that creates a new object and copies all property values ​​of the original object to the new object. However, if there are properties in the original object that refer to other objects, then the shallow copy will only copy the reference without making a deep copy of the referenced object. That's why it's called a "shallow" copy.

Here is an example of shallow copy using JavaScript:

function shallowCopy(obj) {
    
    
  if (obj === null || typeof obj !== 'object') {
    
    
    return obj;
  }

  let copy = obj.constructor();

  for (let key in obj) {
    
    
    if (obj.hasOwnProperty(key)) {
    
    
      copy[key] = shallowCopy(obj[key]);
    }
  }

  return copy;
}

This function first checks whether the input object is empty or not of object type. If so, it returns the input directly. Then, it creates a new object and initializes it using the constructor. Next, it iterates over all properties of the input object and, if the property is an object, makes a recursive shallow copy.

deep copy

A deep copy is different from a shallow copy in that it creates a new object and completely copies all the properties of the original object and the objects it references to the new object. This means that if the original object has a property that references another object, a deep copy will copy the referenced object instead of just copying the reference.

Here is an example of deep copying using JavaScript:

function deepCopy(obj) {
    
    
  if (obj === null || typeof obj !== 'object') {
    
    
    return obj;
  }

  let copy = obj.constructor();

  for (let key in obj) {
    
    
    if (obj.hasOwnProperty(key)) {
    
    
      if (typeof obj[key] === 'object' && obj[key] !== null) {
    
    
        copy[key] = deepCopy(obj[key]);
      } else {
    
    
        copy[key] = obj[key];
      }
    }
  }

  return copy;
}

This function is similar to the one above, but it adds an extra check to confirm that the property is an object and that the object is not empty. If so, perform a recursive deep copy; if not, perform a direct assignment.

Comparison of deep copy and shallow copy

Deep copy and shallow copy each have their advantages and disadvantages. Shallow copy is simpler and faster, but it may lead to data inconsistency. Although deep copy is more complex and slower, it can ensure data integrity.

When dealing with simple data types (such as numbers and strings), shallow copy can meet the needs. However, when dealing with complex data types (such as objects, arrays), especially when these data may be modified, we usually need to use deep copies to ensure the correctness of the data.

Summary and future development

In front-end development, data copying is an important operation. Deep copy and shallow copy are two commonly used data replication methods, each with its own advantages and disadvantages. According to the specific application scenarios and needs, we need to choose the appropriate replication method.


With the development of front-end technology, more efficient and safer data replication methods may be proposed. For example, shallow copying can be achieved using Object.assign() or the spread operator (…), and shallow copying can be achieved using JSON.stringify(), but these methods may have problems when dealing with complex data structures. Therefore, future research may focus on how to deal with these complex situations more effectively.

Guess you like

Origin blog.csdn.net/weixin_46254812/article/details/132613427