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

Case Analysis of Deep copy of javascript

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

Share

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

Most people do not understand the knowledge points of this "javascript deep copy case analysis" article, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "javascript deep copy case analysis" article.

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.

The above is about the content of this article "javascript Deep copy case Analysis". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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

Internet Technology

Wechat

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

12
Report