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 apply javascript Deep copy

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

Share

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

This article introduces the relevant knowledge of "how to apply deep copy of javascript". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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.

This is the end of "how to use javascript Deep copy". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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