I saw some code defines the methods in the prototype this way:
function Class() {...}
(function() {
this.method1 = function() {...};
this.method2 = function() {...};
}).call(Class.prototype);
What is the benefit of this construct versus others? Here are some other examples:
function Class() {...}
Class.prototype = {
method1: function() {...},
method2: function() {...}
};
or
function Class() {...}
Class.prototype.method1 = function() {...};
Class.prototype.method2: function() {...};
or
function Class() {...}
Class.prototype = Object.create({...}, {...});
Class.prototype.constructor = Class;
the following I know is a bit less efficient in term of memory because each instance has its own copy of method1 and method2:
function Class() {
var privateVar = 0;
this.method1 = function() {...};
this.method2 = function() {...};
}
of course the benefit of this construct is that the methods can access private variables.