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 use $.extend to deeply copy objects in jQuery

2025-02-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "how to use $.extend deep copy object in jQuery". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use $.extend deep copy object in jQuery" can help you solve the problem.

Syntax: jQuery.extend ([deep], target, object1 [, objectN])

The parameter corresponding to the deep and shallow copy is [deep], which is optional and is true or false. The default is false (shallow copy), and false cannot be displayed and written out. If you want to write, you can only write true (deep copy)

Test examples:

Var object1 = {}; var object2 = {mm:333}, object1--- 100}; console.log ('original object1--- >' + JSON.stringify (object1)); console.log ('original object2--- >' + JSON.stringify (object2)); $.extend (true,object1, object2); object1.b.mm = 600 console.log ('new object1--- >' + JSON.stringify (object1)) Console.log ('New object2--- >' + JSON.stringify (object2))

Test result

As you know from the test results, $.extend (true,object1, object2) in jQuery can deeply copy objects, and after copying, change the property values of one of the objects without affecting the other.

And then continue to understand deeply.

Test example

Var object1 = {a: 0, b: {gg: 11, mm: 22}}; var object2 = {b: {mm: 333}, c: 100}; / / shallow copy / / object1--- > {"apple": 0, "banana": {"price": 200}, "cherry": 97, "durian": 100} / / object2's banner overrides object1's banner, but the weight attribute is not inherited / / $.extend (object1, object2) / / Deep copy / / object1--- > {"apple": 0, "banana": {"weight": 52, "price": 200}, "cherry": 97, "durian": 100} / / object2 banner overrides object1's banner, but the weight attribute is also inherited by yo $.extend (true,object1, object2); console.log ('original object1--- >' + JSON.stringify (object1)); console.log ('original object2--- >' + JSON.stringify (object2)) Object1.b.mm = 600 position console.log ('new object1--- >' + JSON.stringify (object1)); console.log ('new object2--- >' + JSON.stringify (object2))

Test result

That's all for "how to use $. Extend deep copy objects in jQuery". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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