Implementing a Helper Function
If you find yourself needing to repeat this process in multiple places, it may be beneficial to create a helper function to streamline the task:
function extendPrototype(constructor, methods){
for(var key in methods) {
if(methods.hasOwnProperty(key)) {
constructor.prototype[key] = methods[key];
}
}
}
var controller = function(){
//constructor
};
extendPrototype(controller, {
method1 : function(){
//Prototype method1
},
method2: function(){
//Prototype method2
},
method3 : function(){
//Prototype method3
}
});
return controller;
Utilizing jQuery's Extend Method
It's worth mentioning jQuery's extend method as it offers more functionality compared to the custom helper function mentioned earlier:
var controller = function(){ /* ctor */};
return $.extend(controller.prototype,{
method1 : function(){
//Prototype method1
},
method2: function(){
//Prototype method2
},
method3 : function(){
//Prototype method3
}
});
Alternative Libraries
Other libraries like underscore's extend method or Lo-Dash's assign method offer similar functionalities.