Possible Duplicate:
Use of ‘prototype’ vs. ‘this’ in Javascript?
Defining prototype methods inside the constructor
is there any difference between these two definitions?
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);
}
}
All prototype methods are inside the Task definition, or?
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);
}
I'm not sure wether there is a difference or not. From the OOP view the inside definition looks better.
Thanks!