Javascript Shallow copy of undefined object?

I just started learning Javascript about a week ago, so bear with me. I am trying to make a basic physics engine using quadrants and nodes as the basis for all objects. However, in this piece of code

102 Quadtree.prototype.insert= function(self, object){
103     if (object == null){
104         return;
105     }
106     var objectx,
107         objecty,
108         objectw,
109         objecth;
110     objectx = object.getDim()[0];
111     objecty = object.getDim()[1];
112     objectw = object.getDim()[2];
113     objecth = object.getDim()[3];
114     var cn = root;
115     if(cn == undefined){
116         console.log("asdgalkjh");
117     }

      

cn (current node) returns undefined. The object is also null. I know my problem comes from not knowing how javascript objects work in detail. Can someone please give me a summary or point me where to read? Thank.

here is my node and quadtree class for reference

94 function Quadtree(width,height){
95     this.width = width;
96     this.height = height;
97     this.MAX_LEVELS = 4;
98     this.MAX_OBJ = 2;
99     var root = new Node(0,0,width,height);
100     root.split();
101 }

13 function Node(x,y,width,height){
14     var x,
15         y,
16         width
17         height;
18     this.x = x;
19     this.y = y;
20     this.width = width;
21     this.height = height;
22     var hash = 0;
23     var objects = new Array();
24     var parent = null;
25     var child1 = null;
26     var child2 = null;
27     var child3 = null;
28     var child4 = null;
29 }

      

I realize that maybe I am wrong, I am coming from a heavy java background and the javascript objects are passed by calling "by reference" which is confusing as well. Any help is greatly appreciated.

+3


source to share


1 answer


You are trying to access a private variable root

only accessible in the constructor QuadTree

.



If you want to use it anywhere else, you will have to either make a getter, or put it on public elements (i.e. this.root = new Node(0,0,width,height);

) and then access it withthis.root

+1


source







All Articles