How can I have a reference to an object in javascript? I have the following code:
// define nodes
var NodeA = new Object();
var NodeB = new Object();
NodeA = {
name: "A",
children: [
NodeB
]
}
NodeB = {
name: "B",
children: []
};
var test = NodeA.children[0].name;
// How can I make test = "A" ??? <----------------
alert(test);
I know that if I create NodeB before creating NodeA it will solve my problem.
NodeBdoes resolve to a reference to an object but you are creating a completely new object withNodeB = {...}(and happen to assign it to the same variable). Variables and values are two different things.