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

What is javascript Deep copy and how to use it

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you what javascript deep copy is and how to use the relevant knowledge points, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

To talk about the copy of JavaScript, you have to talk about value passing and reference passing in javascript.

There is no specific syntax in javascript to specify which parameters are passed by reference, while other languages do, such as ref in C# and & in PHP.

This is also one of the many disadvantages of javascript.

Let's take a look at the following code:

/ / value pass var I = 3 var j = I * * j = 4; [xss_clean] (I); / / 3Accord / reference pass var m = [1]; var n = m * n [0] = 2; [xss_clean] (n [0]); / / 2

Note that only simple types in javascript are value passing, while other complex types such as arrays and objects are passed by reference by default.

What if we need to copy an object? You must define the method yourself:

/ / Deep copy function, in fact, the value is passed function cloneObject (srcobj) {var tarobj=new Object (); for (var key in srcobj) {/ / determines whether the object tarobj [key] = typeof srcobj [key] = = 'object'?cloneObject (srcobj [key]): srcobj [key];} return tarobj } / / verify the use of the deep copy function / / Test case: var srcObj = {a: 1, b: {b1: ["hello", "hi"], b2: "JavaScript"}}; var abObj = srcObj;// reference passing var tarObj = cloneObject (srcObj); srcObj.a = 2transfersrcObj.b.b1 [0] = "Hello"; console.log (abObj.a); / / 2console.log (abObj.b.b1 [0]) / / Hello, indicating that normal = is a kind of reference passing console.log (tarObj.a); / / 1console.log (tarObj.b.b1 [0]); / / "hello", indicating that the deep copy we defined is value passing

In fact, by instantiating a new object, it opens up a new memory space in the heap so that the variable name in the stack points to the new content in the heap.

These are all the contents of the article "what javascript Deep copy is and how to use it". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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

Internet Technology

Wechat

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

12
Report