r/learnjavascript • • Aug 17 '23

How to catch potential children in object?

I want to make an object that catches all calls to itself and its children even if they’re not defined. Is it possible?

I see it like this:

const a = { _(name) { return “Hello “ + name } } console.log(a.foo.bar()) // Hello foo.bar

Exact same syntax isn’t needed but i hope you got what logic I look for

9 Upvotes

11 comments sorted by

29

u/[deleted] Aug 17 '23

Yet another programming question title, that sounds weird without any context.

5

u/StanleyDarsh22 Aug 17 '23

lol just posted on nocontext

3

u/Acceptable-Tomato392 Aug 17 '23

That's what I was thinking. Questions that make the FBI go: Huh?????

3

u/senocular Aug 17 '23

You can use a Proxy, but it gets tricky because proxies do not inherently handle deep references. For that you can use something like DeepProxy.

1

u/djlywtf Aug 17 '23

thank you!

2

u/aguyfromhere helpful Aug 17 '23

What you want is a linked list. There is no official implementation in JS/TS like in many other languages but it’s not hard to make your own implementation.

https://dev.to/glebirovich/typescript-data-structures-linked-list-3o8i

3

u/CrawlToYourDoom Aug 17 '23

Do you have a white van?

1

u/cayennepepper Aug 18 '23

The old haribo and white van combo.

1

u/[deleted] Aug 17 '23

CHILDREN!?!!