I’m trying to create a List Object in jquery, I’m using the following code but it’s not working, I found this example so I wrote a similar thing.
function Node(left, right, content)
{
this.left = left;
this.right = right;
this.content = content;
}
var n1 = new Node('', n2, 'content content content');
var n2 = new Node(n1, n2, 'content content content');
var n3 = new Node(n2, '', 'content content content');
console.log(box2);
Now, if i call n1.right it return undefined.
can help me please.
You are passing
n2tonew Nodebefore it has a value, hence it’sundefined. You cannot pass the node to another node before it exists.An easy fix would be to “link” the nodes after you created them:
Though you’d probably want some getter and setter methods to properly handle insertions of new nodes into the list.
Learn more about how objects work: MDN – Woking with objects.
This could be an XY problem though, in which case you’d have to explain what problem you are actually trying to solve. If you just want to create a list of objects, you can use an array [MDN].