0

So I'm writing a webpage that contains a few nested objects.

var Object = {
    //first nested object
    nested1:{
        //nested object within nested1
        moreNested1:{
            //some values
            a:1,
            b:2
        }

    },

    //second nested object
    nested2:{
        //nested object within nested2
        moreNested2:{
            //some values
            x:1,
            y:2
        }

    }

};

As you can see, there are nested objects within nested objects, and there are values stored in moreNested1 and moreNested2.

My problem is trying to access a and b using a function that is located in moreNested2. Using the "this" keyword, I've written a function that tries to access and analyse a and b.

var Object = {
//first nested object
nested1:{
    //nested object within nested1
    moreNested1:{
        //some values
        a:1,
        b:2
    }

},

//second nested object
nested2:{
    //nested object within nested2
    moreNested2:{
        //some values
        x:1,
        y:2,

        //the function that accesses a and b from moreNested1
        foo:function() {
            //code that does not work
            var a = self.this.this.a;
            var b = self.this.this.b;

            //logs a and b
            console.log("a is " + a + " and b is " + b)

        }
    }

}

};

What I am trying to do is access a and b, two values stored within moreNested1, from a function in moreNested2. I just need a pointer on how to access some values in a situation like this.

Thank you. If you could rewrite the code and explain how and why it works, that would be wonderful.

jonathanhuo11
  • 678
  • 1
  • 5
  • 10
  • possible duplicate of [How does the "this" keyword work?](http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) – elixenide May 02 '15 at 13:31
  • Where is `self` defined? – Davin Tryon May 02 '15 at 13:31
  • Please provide less abstraction from your actual use case. What are you trying to accomplish with this object? An object needs nothing special to reference itself. If you are trying to add a calculated property based on other properties then you could use `Object.prototype.defineProperty()` with a handler for your calculation or `foo()` in your example. – Josh May 02 '15 at 14:06
  • I'm an amateur programmer who has been making games and interactions with code for a while now. I've used Unity3D (I'm good with C#), MS Visual Basic C, Java, and HTML5. Recently, I've been into web games that use HTML, JS and JQuery's stuff to make some incremental/idle/strategy games. Its mostly a hobby. So yeah, I'm making a game with objects and nested objects. I've make a couple successful games already, but the project I'm currently working on is very big. Its called World War Potato, a turn based incremental 2 player strategy game. – jonathanhuo11 May 04 '15 at 00:58

1 Answers1

2

Using 'this' won't help - I think you'll need to access the properties from the top level.

var thing = {
//first nested object
nested1:{
    //nested object within nested1
    moreNested1:{
        //some values
        a:1,
        b:2
    }

},

//second nested object
nested2:{
    //nested object within nested2
    moreNested2:{
        //some values
        x:1,
        y:2,

        //the function that accesses a and b from moreNested1
        foo:function() {
            var a = thing.nested1.moreNested1.a;
            var b = thing.nested1.moreNested1.b;

            //logs a and b
            console.log("a is " + a + " and b is " + b);

        }
    }

}
};

thing.nested2.moreNested2.foo();

You can run the code on JSFiddle: https://jsfiddle.net/a6ehfbe5/

prdnr
  • 43
  • 5