Advantages of using prototype, vs defining methods straight in the constructor? [duplicate]

I am wondering if there are any advantages of using any of these over the other, and which way should I go?

Constructor approach:

var Class = function () {

    this.calc = function (a, b) {
        return a + b;
    };

};

Prototype approach:

var Class = function () {};

Class.prototype.calc = function (a, b) {
    return a + b;
};

I don’t like that, using the prototype, method definitions are separated from the class, and I’m not aware if there is any specific reason I should use this over just the first approach.

Also, is there any benefit of using a function literal to define a “class”, over just function definition:

var Class = function () {};

vs

function Class () {};

Thanks!

3 Answers
3

Leave a Comment