JS function: prototype overriding vs prototype extension

Viewed 114

Sometimes I see such JavaScript code:

funciton MyClass () {
    // some initialization
}

MyClass.prototype = {
    constructor: MyClass,

    // other functions
}

What is the benefit in overriding function's prototype vs just extending prototype like this:

function MyClass () {
    // some initialization
}

MyClass.prototype.doStuff = function () {
    // some code
}

In the first example (overriding prototype) we must in addition specify constructor manually, but in the second example constructor will point to the function automatically. It seems to me that the first example is a bit better, because we do not need to specify constructor manually

So why do some people use first way? Is there any advantage?

Thanks

1 Answers
Related