Javascript get parent nested object?

I have an object like this:

obj = { subobj1: { }, subobj2: { func1: function(){ }, func2: function(){ } }, subobj3: { func3: function(){ }, func4: function(){ } }, } 

How can I call func1 from func4 without calling obj.subobj2.func1 ()?

+4
source share
7 answers

You can't for sure. You do not have the right to know in which objects your function exists.

Note that this may be several: you could write this after your existing code:

 var obj2 = {some:obj.subobj3}; 

Thus, there can be no unique link (and no link is available) from the property value to the object containing it.

Now, if you are happy with the link created when you created the object, you can use the factory to create your object:

 obj = (function(){ var parent = { subobj1: { }, subobj2: { func1: function(){ }, func2: function(){ } }, subobj3: { func3: function(){ }, func4: function(){ parent.subobj2.func1(); } } }; return parent; })(); 

Then you can call

 obj.subobj3.func4(); 

Demonstration


EDIT

I see that you gave the OOP tag to your question. You should know that the template I gave is more often used to define modules. OOP in javascript is most often done using new and prototype to enable instance sharing methods and inheritance. As you probably need modules, not OOP, you seem to be fine.

See this introduction .

+12
source

Here you can add .parent to any sub-object with recursive init:

 var obj = { init: function() { for (var i in this) { if (typeof this[i] == 'object') { this[i].init = this.init; this[i].init(); this[i].parent = this; } } return this; }, subobj1: { }, subobj2: { func1: function(){ console.log('hey'); }, func2: function(){ } }, subobj3: { func3: function(){ }, func4: function(){ this.parent.subobj2.func1(); } } }.init(); obj.subobj3.func4(); 

With this solution, you can also use .parent as many times as your investment requires, for example, if you had two levels of nesting:

 this.parent.parent.subobjX.funcY(); 

http://jsbin.com/yuwiducadoma/1/watch?js,console

+2
source

As others have said, with a simple object it is not possible to find a parent from a nested child.

However, this is possible if you use the recursive ES6 Proxies as helpers.

I wrote a library called ObservableSlim , which, among other things, allows you to switch from a child to a parent.

Here is a simple example ( jsFiddle demo ):

 var test = {"hello":{"foo":{"bar":"world"}}}; var proxy = ObservableSlim.create(test, true, function(changes) { console.log(JSON.stringify(changes)); }); function traverseUp(childObj) { console.log(JSON.stringify(childObj.__getParent())); // returns test.hello: {"foo":{"bar":"world"}} console.log(childObj.__getParent(2)); // attempts to traverse up two levels, returns undefined because test.hello does not have a parent object }; traverseUp(proxy.hello.foo); 
+1
source

It's impossible. The properties of an object receive the object through which they are set. JavaScript does not imply that the root object is global, and therefore you cannot use the alias obj.subobj2.func1 as func1 or in any way shorter. If you call this function too often, try setting a variable instead.

0
source

You cannot do this directly, since there is no way to "approach" the hierarchy of objects as you can with ".." in the file system.

What you can do is have variables that point directly to subobjects or subfunctions, so you don't have to go through the hierarchy to call them. The following is a general template for creating Javascript modules:

 obj = (function(){ var obj1 = {...} var obj2 = {...} var func3 = function(){...}; var func4 = function(){...}; return { subobj1: obj1, subobj2: obj2, subobj3: { func3: func3, func4: func4 } } }()); 

In this example, internal functions can access obj1 , obj2 , func3 and func4 directly from their variables. The self-service function makes these internal variables private and hidden from the outside, and the return statement allows you to export only the functions that you want to publish.

0
source

Here is more ActionScript as it is easy to understand:

 function MainObject() { this.x = 100; this.y = 50; this.second = new SecondObject(this); } function SecondObject(p) { this.parent = p; } var firstobject = new MainObject(); // Saving a reference to SecondObject. var secondObject = firstobject.second; // Because of parent variable, you can access second object parent without needing to have the parent object in a variable. console.log(secondObject.parent.x); console.log(secondObject.parent.y); 

Just remember that an object cannot have multiple parents.

0
source

This is not possible with your implementation of the object. Depending on your needs, the following implementation may be useful:

 obj = function(){ var obj = { subobj1: { }, subobj2: { func1: function(){ console.log('func1'); }, func2: function(){ } }, subobj3: { func3: function(){ }, func4: function(){ this.parentObject.subobj2.func1(); } } } obj.subobj3.parentObject = obj; /* //You can also init parentObject reference of the rest of the nested objects if you need obj.subobj1.parentObject = obj; obj.subobj2.parentObject = obj; */ return obj; }(); obj.subobj3.func4(); 

The idea is that nested objects cannot know who their parent is, but the parent can tell their children who he is (but then the init code is needed, as you can see in my implementation).

You can test it here: http://jsbin.com/eVOpITom/1/edit

0
source

Source: https://habr.com/ru/post/1448009/


All Articles