function A () { this.helloWorld = function () {return 'hello world'}; }; a = new A();
vs.
function B () {}; B.prototype.helloWorld = function () {return 'hello world'}; b = new B();
Both constructors achieve the same result - their instances a and b both have the methods helloWorld.
Some of the differences are:
1. With constructor A, whenever you create a new instance, you create a new function, hence you have both performance and memory inefficiencies. With Constructor B, the helloWorld function is created just once, and every instance of B automatically inherits this single instance of the function helloWorld, hence we have performance and memory efficiency.
2. Using inheritance through the prototype of the constructor functions also gives more flexibility and simplicity in sharing common functions and modules.
To demonstrate the concept, I created this code example:
http://jsbin.com/utuso3/edit [Click on 'Preview' button to run the example code]
I hope that made sense.
"Assigning a member function to a prototype should not be confused with assigning an anonymous function to a member variable of a class, which only adds the function to a particular instance of the class"
I thought that by setting this.somefn = function() { blah; }; in the constructor, you are adding the function to the prototype of the object, with the name "somefn". At least, I have implemented a fair amount of code this way and used classical JS inheritance without any apparent issues, I would like to know what the difference is both practically and theoretically.