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 JavaScript gets the parent of an attribute based on the attribute

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

Share

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

This article mainly introduces "JavaScript how to get the parent of the attribute according to the attribute". In the daily operation, I believe that many people have doubts about how the JavaScript obtains the parent of the attribute according to the attribute. Xiaobian consulted all kinds of data and sorted out a simple and useful method of operation. I hope it will be helpful to answer the question of "how JavaScript obtains the parent of the attribute according to the attribute". Next, please follow the editor to study!

Needless to say, the code function mObj (Obj) {var obj = new Proxy (Obj, {set: function (obj, pro, val) Pro) {if (typeof val = = 'object') {val.parent = Pro val = mObj (val)} obj [pro] = val return val }}) return obj}

We just need to initialize the object we need to proxy with the mObj method

Var obj = mObj ({}) add attributes to it

Note! You can only add an empty object, otherwise its properties will lose the proxy

Obj.a = {} obj.b = {} obj.a.aa = {} obj.b.bb = {} console.log (obj) / / Proxy {a: Proxy, b: Proxy}

Proxy is the proxy object.

Try to use obj.parent to get its parent console.log (obj.a.parent,obj.b.parent) / / Proxy {a: Proxy, b: Proxy} Proxy {a: Proxy, b: Proxy} console.log (obj.a.aa.parent,obj.b.bb.parent) / / Proxy {parent: Proxy, aa: Proxy} Proxy {parent: Proxy, bb: Proxy}

You can see that the parent of the property has been successfully acquired, so what can it be used for?

1. Locate the parent obj.a.parent / / Proxy {a: Proxy, b: Proxy} 2. Generate the sibling attribute var p = obj.ap.parent.c = {} obj.c / / Proxy {parent: Proxy} 3. Check whether it is sibling attribute var p1 = obj.a.parent, p2 = obj.b.parentvar p3 = obj.a.aa.parent, p4 = obj.b.bb.parentp1 = = p2 / / truep3 = = p4 / / false4. Locate the highest parent P5 = obj.a.aawhile (p5.parent) {P5 = p5.parent} P5 / / Proxy {a: Proxy, b: Proxy} P5 = = obj / / true so far, the study on "how to get the parent of an attribute based on an attribute" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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