这两个定义有什么区别吗?
function Task(description, value) {
this.id = 0;
this.subId = 0;
this.parent = null;
this.children = new Array();
this.description = description;
this.value = value;
Task.prototype.getNextId = function () {
return this.subId++;
},
Task.prototype.addTask = function (task) {
task.id = this.getNextId();
this.children[task.id] = task;
task.parent = this;
},
Task.prototype.remove = function () {
this.parent.children.remove(this.id);
}
}
所有原型方法都在任务定义中,或者?
function Task(description, value) {
this.id = 0;
this.subId = 0;
this.parent = null;
this.children = new Array();
this.description = description;
this.value = value;
}
Task.prototype.getNextId = function () {
return this.subId++;
},
Task.prototype.addTask = function (task) {
task.id = this.getNextId();
this.children[task.id] = task;
task.parent = this;
},
Task.prototype.remove = function () {
this.parent.children.remove(this.id);
}
我不确定是否有区别。从 OOP 视图来看,内部定义看起来更好。
谢谢!