How to Access Parent Object

access parent object in javascript

You can't.

There is no upwards relationship in JavaScript.

Take for example:

var foo = {
bar: [1,2,3]
}

var baz = {};
baz.bar = foo.bar;

The single array object now has two "parents".

What you could do is something like:

var User = function User(name) {
this.name = name;
};

User.prototype = {};
User.prototype.ShowGreetings = function () {
alert(this.name);
};

var user = new User('For Example');
user.ShowGreetings();

Is there any way to access parent object?

export default {
data: {
a: "a",
b: "b"
},
fn: {
innerFn: () => console.log(default.data.b)
}

}

This might do the trick for you.

JavaScript access parent object attribute

You can use call to set the value of this:

parent.child.displayA.call(parent); // 5

You may also be interested in binding it:

parent.child.displayA = function(){
console.log(this.a);
}.bind(parent);
parent.child.displayA(); // 5

Or you you can just use parent instead of this:

parent.child.displayA = function(){
console.log(parent.a);
};
parent.child.displayA(); // 5

Access parent's parent from javascript object

JavaScript does not offer this functionality natively. And I doubt you could even create this type of functionality. For example:

var Bobby = {name: "Bobby"};
var Dad = {name: "Dad", children: [ Bobby ]};
var Mom = {name: "Mom", children: [ Bobby ]};

Who does Bobby belong to?

Access parent and child variables in method defined in parent - PHP

Instead of having addFoo be called by every sub-constructor, one way would be to have a single addFoos method in the base class that is called by the base constructor, that would append all the $foo values starting from the late static binding class:

class A
{
public static $foo = 'foo';

public $vars = [];

public function __construct()
{
$this->addFoos();
}

private function addFoos()
{
$class = static::class;
do {
$this->vars[] = $class::$foo;
} while ($class = get_parent_class($class));
}

}

class B extends A
{
public static $foo = 'bar';
}

class C extends B
{
public static $foo = 'baz';
}

$a = new A;
print_r($a->vars); // ['foo']

$b = new B;
print_r($b->vars); // ['bar', 'foo']

$c = new C;
print_r($c->vars); // ['baz', 'bar', 'foo']

That method is marked private as it's not supposed to be extended in this scenario (nor called from the outside).

Demo

Better way of child object accessing parent object?

This is already the correct way to do this, in principle. You could make it somewhat fancier, providing an introduceParent() function in the child which checks, if the parent reference is already set, but that doesn't change the core of the matter.

While a child in your data model can belong to only one parent, Javascript doesn't know that. There could be several parents referencing the same child, for example (hearsay is that sometimes happens in nature). Indeed, the child is not an "inner class" of the parent. The child and parent are merely associated, i.e. "they somehow know each other", which is an unspecified relation, but neither is part (or property) of the other.

What is the right way to access parent object in JS?

While nesting classes itself is already a bit hacky, you can get even hackier and use an IIFE to expose the GameManager as a scoped variable:

class GameManager {
instance = { levels: {} }
constructor() {

}

Level = (instance => class {
constructor(name) {
}

getGame = () => instance;
})(this);
}

const game = new GameManager();
var loadScreen = new game.Level('ls');
console.log(loadScreen.getGame());

But again, might be better off not nesting the classes and instead using a factory method, e.g. loadScreen = game.createLevel('ls'). The factory method could then call the non-nested version like new Level(game, 'ls').



Related Topics



Leave a reply



Submit