Get the App
SLTechnology News&Howtos  ›  Development  › 

How JavaScript gets the parent of an attribute based on the attribute

Shulou Source: shulou.com Published: 2022-06-01 13:34:20 09月17日 Update

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!

Tags: Attributes learning objects agents brothers methods more positioning help practical highest successful and then the previous generation that is articles theories knowledge articles websites Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple NVidia MariaDB Linux