Uses existing walls in a second level parentNode

1

I have the following object as an example:

var teste = {
    config : {
         dir: "myDir",
         type:"myType"
    },
    fn : {
         foo : function(){
             console.log(this); //retorna o objeto fn
         },
         bar : function(){
             console.log(this[1].config.dir); // entendo que fn seria this[0]
             console.log(this.this.config.dir); //deveria retornar teste
             console.log(this..config.dir); //não funciona por não ter parametro
         }
    }
}

I need to know if there is any way to access my object that is in a second parentNode to then enter the object config and get the value of the parameter dir .

Today I am forced to call my own object when I find myself in a similar situation. Is there any way to call a relative that is at node levels greater than the parameter node?

    
asked by anonymous 23.06.2015 / 18:23

1 answer

1

I understand that you should define a class to do the service:

function MyClass(config) {
    this.dir = config.dir;
    this.type = config.type;
}

MyClass.prototype.foo = function (){
    console.log(this);
}

MyClass.prototype.bar = function () {
    console.log(this.dir);
}

var myObject = new MyClass({
    dir: "myDir",
    type: "myType",
});

myObject.foo();
myObject.bar();

JSFiddle .

I think you're confusing the concepts - these things like parentNode only exist for elements of the DOM, i.e. bits of the page. If you set your own objects, you have to create any references to parents and children that you want to use by hand.

    
23.06.2015 / 18:57