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.

4
  • possible duplicate of How does the "this" keyword work? Commented May 2, 2015 at 13:31
  • Where is self defined? Commented May 2, 2015 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. Commented May 2, 2015 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. Commented May 4, 2015 at 0:58

1 Answer 1

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/

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

1 Comment

I did not realize you could use the name of the object before it was declared.

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.