0

I'm having an issue getting the structure of my JS code right. I'm basically trying to create a complex object which contains other objects.

What I'm after is basically something like :

object {
    f1: (),
    f2: (),
    objA: {
        objA: {
            v1: ""
        }
    },
    objB: {
        objA: {
            v1: "",
            v2: "",
            f1: ()
        }
    }
}

Or something similar to this. I had something kinda of working, but in the object, I want to be able to reference other parts of the object.

So for instance, with the example above, I'd like to set the variable object.objA.objB.v1 to be equal to the value from object.objA.objA.v1

When I tried this, I just got an error saying it couldn't access objA of undefined.

How can I get around this or restructure it to make it work?

PaReeOhNos
  • 4,338
  • 3
  • 30
  • 41
  • 1
    You cannot access other properties of an object until the object has been initialised, until the final }. Up until that point the object is indeed undefined. I wrote an answer for a similar question here: http://stackoverflow.com/questions/14553639/javascript-and-referencing-one-namespace-property-from-another/14553721#14553721 – Coin_op Jan 29 '13 at 22:56
  • It is normal for object literal, you can't access object that was not yet defined. – VisioN Jan 29 '13 at 22:57
  • yeah that makes sense. Is there any nice ways around this? I don't want to use a function to return the value as I want the data to be overridden at a later point. I only want the code that references the variables to do so when it is invoked? – PaReeOhNos Jan 29 '13 at 23:00
  • In that case you could create the object as a function so you have a constructor. Then in that constructor function you can set all the variables you like as the object has already been initialized. – Coin_op Jan 29 '13 at 23:03
  • In your code there is no `object.objA.objB.v1`, maybe you mean `object.objB.objA.v1`? – Wojciech Bednarski Jan 29 '13 at 23:04

3 Answers3

1

Your example throws an error because it is invalid. Simply object.objA.objB.v1 doesn't exist. But if you want to use the properties from objects which exist you can do it simply by:

var yourObj =  {
objA: {
    objA: {
        v1: "a"
    }
},
objB: {
    objA: {
        v1: "b",
        v2: "c",
    }
}
}

yourObj.objB.objA.v1 = yourObj.objA.objA.v1;
alert(yourObj.objB.objA.v1); 

jsFiddle

DarkAjax
  • 15,955
  • 11
  • 53
  • 65
Mateusz Rogulski
  • 7,357
  • 7
  • 44
  • 62
0

Object literals are not self-referential. You cannot reference something that hasn't been defined yet. This is why you're getting an error.

What you can do is defer definition of self-referential properties after the structure has been initially defined.

So initially define your object:

var obj = {
    a: {
        prop1: 1
    },
    b: 2,
    innerObj: {}
};

Then:

obj.innerObj.a = obj.a;

Keep in mind that this won't work for primitive types like numbers. This will work for objects that have references, like functions and other objects/arrays.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
0

Unfortunately in Javascript when defining object literals you cannot reference other parts of the object to itself. So:

object.objA.objB.v1: object.objA.objA.v1 is not legal. It has no reference to object yet because it's not finished being defined.

What you could do is define the object, and afterward set specific values on it. They would not update automatically though unless they were also objects and therefore being passed by reference.

A possible solution is that object.objA.objB.v1 could be a function that returns the value of object.objA.ovjaA.v1.

earl3s
  • 2,393
  • 1
  • 23
  • 24