JavaScript Class 和构造函数(非 Dojo
JavaScript Class and Constructor (non-Dojo
var TreeNode = function() {
this.x = null;
this.y = null;
this.data = [];
TreeNode = function(x1,y1,object) {
this.x = x1;
this.y = y1;
this.data.push(object);
};
};
我的问题是,如果我创建新的 TreeNode(90,80,"Hallo World");
,它会告诉我 this.data 未定义。谁能帮帮我?
问候
推送前需要创建数据:
TreeNode = function(x1,y1,object) {
this.data = [];
this.x = x1;
this.y = y1;
this.data.push(object);
};
使用这个
var TreeNode = function(x1,y1,object)
{
this.data = [];
this.x = x1;
this.y = y1;
this.data.push(object);
};
var treeNode = new TreeNode(1,2, 'node data');
不清楚你想从你的问题中得到什么,但以下内容确实有效:
function TreeNode (x ,y, obj) {
this.x = x;
this.y = y;
this.data = [obj];
}
var aTreeNode = new TreeNode(1, 2, 'hello world');
console.log(aTreeNode.data); //['hello world']
从上面的代码看来,您正在创建 2 个 TreeNode
构造函数 - 一个在另一个内部。这是故意的吗?
var TreeNode = function() {
this.x = null;
this.y = null;
this.data = [];
TreeNode = function(x1,y1,object) {
this.x = x1;
this.y = y1;
this.data.push(object);
};
};
我的问题是,如果我创建新的 TreeNode(90,80,"Hallo World");
,它会告诉我 this.data 未定义。谁能帮帮我?
问候
推送前需要创建数据:
TreeNode = function(x1,y1,object) {
this.data = [];
this.x = x1;
this.y = y1;
this.data.push(object);
};
使用这个
var TreeNode = function(x1,y1,object)
{
this.data = [];
this.x = x1;
this.y = y1;
this.data.push(object);
};
var treeNode = new TreeNode(1,2, 'node data');
不清楚你想从你的问题中得到什么,但以下内容确实有效:
function TreeNode (x ,y, obj) {
this.x = x;
this.y = y;
this.data = [obj];
}
var aTreeNode = new TreeNode(1, 2, 'hello world');
console.log(aTreeNode.data); //['hello world']
从上面的代码看来,您正在创建 2 个 TreeNode
构造函数 - 一个在另一个内部。这是故意的吗?