Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize assignment, shallow copy and Deep copy in js

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to achieve assignment, shallow copy and deep copy in js. It is very detailed and has a certain reference value. Interested friends must read it!

1. Js memory

Js memory, or memory for most languages, is divided into stack and heap. The variable value of the basic data type is assigned on the stack, the variable value of the reference data type is assigned on the heap, and the address of the object in the specific heap is stored in the stack.

2. Assignment

For basic data types, the assignment operation is a copy, that is, new and old variables do not affect each other.

Var a = 1 position var b = a * b = 2 * console.log (b); / / 2

For reference data types, the assignment operation simply adds a variable to the stack that points to the object in the heap, that is, copying the reference address. The new and old variables will influence each other, that is, if the object value is changed on the new variable, the corresponding value of the old variable will also change.

Var a = {name: "mike"}; var b = a b.name = "jack"; console.log (a); / / {name: "jack"} 3, shallow copy

For basic data types and data without nested objects, it is a copy operation, and the old and new variables do not affect each other.

Var a = {name: "mike"}; var b = {}; b.name = a.name.b.name = "jack"; console.log (a) / / {name: "mike"}

However, for data with nested objects, the shallow copy copies only the first-tier objects, and the deep-seated value is still the copy reference address.

Var a = {name: "mike", language: {first: "english", second: "chinese"}}; var b = {}; b.name = a.nametrab.name = "jack"; b.language = a.exploragetb.classiage.first = "japanese" console.log (a) / / {language: {first: "japanese", second: "chinese"}}

Js implements a shallow copy with the idea of iterating through each attribute of target and assigning the name and value of the attribute to the new variable.

If you understand the meaning of assignment, then in the fourth line of the code, when the value of target [key] at this time is an object, assigning a new variable by assignment is essentially copying the address of the reference data type in the heap, it is not difficult to understand why shallow copy has different results for whether it is a nested object.

Function shallowCopy (target) {let result = {}; for (const key in target) {result [key] = target [key];} return result;} 4, Deep copy

A deep copy is a complete copy, and there is no interaction between old and new variables.

There are different ways to handle whether the parameter is an object, if it is an object, assign a value to each property and value of the object and then deal with it recursively; otherwise, it is returned directly.

Function clone (target) {if (typeof target = "object") {/ / determine whether it is an array let result = Array.isArray (target)? []: {}; for (const key in target) {result [key] = clone (target [key]);} return result;} else {return target }} these are all the contents of the article "how to achieve assignment, shallow copy and Deep copy in js". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report