1

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.

1
  • NodeB does resolve to a reference to an object but you are creating a completely new object with NodeB = {...} (and happen to assign it to the same variable). Variables and values are two different things. Commented Apr 28, 2016 at 17:52

3 Answers 3

4

You can use Object.assign to assign properties to NodeB instead of setting NodeB to be a reference to a entirely new object:

var NodeB = {};

var NodeA = {
    name: "A",
    children: [
        NodeB
    ]
}

Object.assign( NodeB, {
    name: "B",
    children: []
} );

var test = NodeA.children[0].name;
alert(test);

Note that for Internet Explorer of Object.assign support you would need to use the Polyfill. You could also just assign the properties to the existing object instead of using object literal notation to create a new one:

var NodeB = {};

var NodeA = {
    name: "A",
    children: [
        NodeB
    ]
}

NodeB.name = "B";
NodeB.children = [];

var test = NodeA.children[0].name;
alert(test);

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

2 Comments

Should it be: Object.assign?? You put a comma there by mistake I think.
Note that .assign's compatibility on browsers isn't that great at the moment.
1

Why not use a function constructor:

function Node(name) {
    this.name = name,
    this.children = [];
}

var nodeA = new Node('A'),
    nodeB = new Node('B');

nodeA.children.push(nodeB);

document.write(nodeA.children[0].name);
document.write('<pre>' + JSON.stringify(nodeA, 0, 4) + '</pre>');

3 Comments

Nina, what's the difference? If you then assign a new value to nodeB, nodeA.children won't change.
@isvforall, if you assign a value to a variable, tha last value/reference is lost. or do i miss something?
OP change the NodeB after constructing the NodeA.children, if you change the NodeB(assign a new value) you will have the same affect as OP
0

When you assign a new value you are removing the reference of the object and then you cannot access to the object that inside children array

NodeB = {
    name: "B",
    children: []
};

You can use dot notation to assign a new properties:

NodeB.name = "B";
NodeB.children = [];

Example:

var NodeA = new Object();
var NodeB = new Object();

NodeA = {
    name: "A",
    children: [
        NodeB
    ]
}

NodeB.name = "B";
NodeB.children = [];

var test = NodeA.children[0].name;

alert(test);

Comments

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.