¿Cómo llamo el atributo y las funciones del objeto primario en javascript? Por ejemplo, si tuviera un objeto gen0 con un objeto hijo gen1 que tuviera un objeto hijo gen2. ¿Cómo gen1 llamaría a sus padres nombres y funciones? ¿Cómo llamaría gen2 a sus funciones de padres y abuelos (sin usar prototipos)?

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
1N5818 4 mar. 2018 a las 09:14

4 respuestas

La mejor respuesta

No hay nada que vincule automáticamente los objetos con los objetos que contienen referencias a ellos. Si quieres eso, debes hacerlo explícitamente en tu código.

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();
3
Barmar 4 mar. 2018 a las 06:24
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();
0
Kevin 4 mar. 2018 a las 06:26
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();
1
Farhad Yasir 4 mar. 2018 a las 06:34

Deberías abordar esto de una manera diferente. Una forma de hacerlo sería utilizar clases y objetos con relaciones (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
2
Ahmad 4 mar. 2018 a las 06:34