In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces "javascript deep copy application case analysis" in detail, the content is detailed, the steps are clear, and the details are handled properly. I hope this "javascript deep copy application case analysis" article can help you solve your doubts.
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.
After reading this, the article "javascript Deep copy Application example Analysis" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.