3

How do I call parent object attribute and functions in javascript? For example, if I had an object gen0 with a child object gen1 which had a child object gen2. How would gen1 call its parents names and functions? How would gen2 call its parents and grandparents functions (without using prototypes)?

gen0 = {
    name: "gen0",
    gen0Func: function() {
        console.log("gen0Func");
        console.log("My name is " + this.name);
    },
    gen1: {
        name: "gen1",
        gen1Func: function() {
            console.log("gen1Func");
            console.log("My name is " + this.name);
            console.log("My parents name is " + this.parent.name); // Doesn't work.
        },
        gen2: {
            name: "gen2",
            gen2Func: function() {
                console.log("gen2Func");
                console.log("My name is " + this.name);
                console.log("My parents name is " + this.parent.name); // Doesn't work.
                console.log("My grandparents name is " + this.parent.parent.name); // Doesn't work.

            }  
        }
    }
}
3

4 Answers 4

3

There's nothing that automatically links objects back to the objects that contain references to them. If you want that, you need to do it explicitly in your code.

gen0 = {
    name: "gen0",
    gen0Func: function() {
        console.log("gen0Func");
        console.log("My name is " + this.name);
    },
    gen1: {
        name: "gen1",
        gen1Func: function() {
            console.log("gen1Func");
            console.log("My name is " + this.name);
            console.log("My parents name is " + this.parent.name); // Doesn't work.
        },
        gen2: {
            name: "gen2",
            gen2Func: function() {
                console.log("gen2Func");
                console.log("My name is " + this.name);
                console.log("My parents name is " + this.parent.name); // Doesn't work.
                console.log("My grandparents name is " + this.parent.parent.name); // Doesn't work.

            }  
        }
    }
}

gen0.gen1.parent = gen0;
gen0.gen1.gen2.parent = gen0.gen1;

gen0.gen1.gen2.gen2Func();

Sign up to request clarification or add additional context in comments.

Comments

2

You should approach this in a different way. One way I would do this, is to use classes and objects with relations (parent, children, etc)..

class Gen {
	constructor(name,parent) {
		this.name = name;
		this.parent = parent;
	}
	
	sayHi() {
		console.log('Hi!.. I am ' + this.name);
	}
	
}

var gen0 = new Gen('gen0');
var gen1 = new Gen('gen1',gen0);
var gen2 = new Gen('gen2',gen1);

gen0.sayHi(); // Hi!.. I am gen0
gen1.sayHi(); // Hi!.. I am gen1
gen1.parent.sayHi(); // Hi!.. I am gen0
gen2.parent.parent.sayHi(); // Hi!.. I am gen0

Comments

1

var gen0 = {
    name: "gen0",
    gen0Func: function() {
        console.log("gen0Func");
        console.log("My name is " + this.name);
    },
    gen1: {
        name: "gen1",
        gen1Func: function() {
            console.log("gen1Func");
            console.log("My name is " + this.name);
            console.log("My parents name is " + this.parent.name);
        },
        gen2: {
            name: "gen2",
            gen2Func: function() {
                console.log("gen2Func");
                console.log("My name is " + this.name);
                console.log("My parents name is " + this.parent.name); // Doesn't work.
                console.log("My grandparents name is " + this.parent.parent.name);

            }  
        }
    },
    init: function() {
       this.gen1.parent = this;
       this.gen1.gen2.parent = this.gen1;
       return this;
    }
}.init();

gen0.gen1.gen1Func();
gen0.gen1.gen2.gen2Func();

Comments

0

gen0 = {
    name: "gen0",
    gen0Func: function() {
        console.log("gen0Func");
        console.log("My name is " + this.name);
    },
    gen1: {
        name: "gen1",
        gen1Func: function() {
            console.log("gen1Func");
            console.log("My name is " + this.name);
            console.log("My parents name is " + gen0.name); // Doesn't work.
        },
        gen2: {
            name: "gen2",
            gen2Func: function() {
                console.log("gen2Func");
                console.log("My name is " + this.name);
                console.log("My parents name is " + gen0.gen1.name); // Doesn't work.
                console.log("My grandparents name is " + gen0.name); // Doesn't work.

            }  
        }
    }
}

gen0.gen1.gen2.gen2Func();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.