14

Is there a shorter way to write this:

var controller = function(){ 
    /*--- constructor ---*/
}; 

controller.prototype.function1 = function(){ 
    //Prototype method1
}

controller.prototype.function2 = function(){ 
    //Prototype method2
}

controller.prototype.function3 = function(){ 
    //Prototype method3
} 

return controller

I'm using require.js. I wondered if I can avoid the controller.prototype code repetition.

5
  • You could probably extend the prototype with an object, but why, there's nothing wrong with readability and typing "prototype" for each added prototype. Commented Apr 11, 2014 at 18:43
  • That's what I was wondering about :-) Continuous code repetition always seems suspicious to me. Commented Apr 11, 2014 at 18:45
  • 1
    you could do a ref shortcut pretty simply: var cp=controller.prototype; then cp.function3= ... you can also get fancy with object literals, or if you only need to support evirons that expose function.name, you can get really "classy" Commented Apr 11, 2014 at 18:45
  • That seems indeed like an obvious step forward. Thanks. Commented Apr 11, 2014 at 18:47
  • 1
    Though the answers given are all good, tossing out the use of jQuery's extend method as yet another option. That is a very similar to the protomix function given below. Commented Apr 12, 2014 at 4:02

3 Answers 3

14

With a Helper Function

Even though this is longer than the answer given if you have to do this multiple places might be helpful to define a helper method:

function protomix(constructor, mix){
    for(var i in mix)
      if(mix.hasOwnProperty(i))
          constructor.prototype[i]=mix[i];
}

var controller = function(){
 //constructor
};

protomix(controller, {

   function1 :  function(){ 
       //Prototype method1
   },

   function2:  function(){ 
       //Prototype method2
   },

   function3 : function(){ 
    //Prototype method3
   } 
});

return controller;

Using jQuery's extend method

I thought I should mention jQuery's extend method because it was brought up in a comment and because in general has more functionality than the small helper method defined in the first part of the answer:

var controller = function(){ /* ctor */};
return $.extend(controller.prototype,{

   function1 :  function(){ 
       //Prototype method1
   },

   function2:  function(){ 
       //Prototype method2
   },

   function3 : function(){ 
    //Prototype method3
   } 
});

Other Libraries

Other libraries also have similar functionality built in, such as underscore's extend method or Lo-Dash's assign method

Sign up to request clarification or add additional context in comments.

1 Comment

I accept it, because you are right. I'm planning to use a lot of controllers in my application, so this function will come in very handy. Thanks.
1

With ES6, you can also use the object destructuring.

function Foo() {
}

Foo.prototype = {
  ...Foo.prototype,
  foo() {},
  bar() {},
  baz() {}
}

Comments

0

Object.assign(controller.prototype, { function1: ... })

1 Comment

You are better off using Object.defineProperties

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.