2

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!

Community
  • 1
  • 1
Christian Koch
  • 701
  • 7
  • 16
  • 3
    [Defining prototype methods inside the constructor](http://stackoverflow.com/questions/7115594/defining-prototype-methods-inside-the-constructor) should answer your question (or at least help in understanding the differences). – Felix Kling Dec 02 '12 at 18:31
  • The "inside" is executed every time the constructor is invoked, so you don't want to put prototype method definitions in there. Having its guts hang out is a trademark of JavaScript "classes" `:P` – Šime Vidas Dec 02 '12 at 18:33
  • @DCoder, I don't think so. He's asking about putting prototype methods inside or outside of constructors; I don't see how 'this' is involved. – jonvuri Dec 02 '12 at 18:33
  • 1
    @DCoder: In this case, functions are assigned to `Task.prototype` inside the constructor, not to `this`. These are two different scenarios. Not a duplicate. – Felix Kling Dec 02 '12 at 18:33
  • Thank you very much. I think this http://stackoverflow.com/questions/7115594/defining-prototype-methods-inside-the-constructor is what I searched for. – Christian Koch Dec 02 '12 at 18:37

2 Answers2

1

The prototype of a constructor is an object that is shared among instances created from the constructor.

If you update the properties of the prototype object inside the constructor, all the instances that share that prototype object will reference the latest update.

In your code you won't notice a difference, but it's still wrong. There's no need to keep overwriting the prototyped functions with new identical versions.

If your code changes so that the functions added to the prototype reference local variables in the constructor, then all instances will end up using the prototyped function that references the variables in the newest invocation. This is almost never what you'd want.

If your code changes so that the entire prototype object of the constructor is overwritten, then each instance created will be referencing a different prototype object, and you won't be able to update the prototype of all instances by adding new methods to Task.prototype, because it will just be updating the prototype object of the most recent instance.

I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77
0

The first one assigns the prototype functions each time the constructor is called. So, if you later reassign those somewhere else, they will be overwritten again whenever you construct another base object of that type. This is almost always undesirable.

See this question for a more detailed discussion: Defining prototype methods inside the constructor

Community
  • 1
  • 1
jonvuri
  • 5,738
  • 3
  • 24
  • 32