Uses existing paraments in a second-level parentNode

Asked

Viewed 47 times

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’s any way to access my object that’s in a second parentNode to then enter the object config and take the value of the parameter dir.

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

1 answer

1

I understand that you should set a class to do the job:

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 DOM elements, i.e. page pieces. If you define your own objects, you have to create any references to parents and children that you want to use by hand.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.