我大约一周前才开始学习 Javascript,所以请多多包涵。我正在尝试使用四叉树和节点作为所有对象的支持结构来制作一个基本的物理引擎。 然而在这段代码中
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(当前节点)返回未定义。对象也为空。 我知道我的问题是由于不知道 javascript 对象的详细工作原理而引起的。有人可以给我一个摘要或指出我在哪里阅读吗?谢谢。
这是我的节点和四叉树类供引用
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 }
我意识到我可能做错了很多事情,我来自沉重的 java 背景,javascript 对象是通过“按引用调用”传递的,到目前为止这也很困惑。非常感谢任何帮助。
最佳答案
您正在尝试访问私有(private)变量 root
,它只能在 QuadTree
构造函数中访问。
如果你想在其他任何地方使用它,你必须制作一个 getter 或将它放在公共(public)成员中(即 this.root = new Node(0,0,width,height);
) 然后使用 this.root
关于对象的 Javascript 浅拷贝未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30811344/