0

I have a situation like this:

"render": function(a, b) { // do stuff }

I want to change this to a named function:

"render": foo

function foo(a, b) { // do stuff }

The thing is, the function can do different things in different situations, so I want to add another parameter like this:

function foo(a, b, type)

How do I call this function and still pass in the original parameters? I have no control over the code that calls this method so I don't know where a and b come from.

LeonCS
  • 33
  • 1
  • 4
  • So what is the issue? You want to call the original function? If they do not pass in type it is undefined... `if (type===undefined){ console.log("no type"); }` – epascarello Sep 05 '14 at 13:14
  • You can just call it `foo(x, y)` regardless how many parameters the function has (`type` would be `undefined` then). What does not work? Are you looking for [method overloading](http://stackoverflow.com/a/12694605/1048572)? – Bergi Sep 05 '14 at 13:18
  • possible duplicate of [Is there a better way to do optional function parameters in Javascript?](http://stackoverflow.com/questions/148901/is-there-a-better-way-to-do-optional-function-parameters-in-javascript) – Armel Larcier Sep 05 '14 at 13:20

2 Answers2

4
if (typeof type === 'undefined') {
    // 2 parameters
} else {
    // 3 parameters
}
targrik
  • 41
  • 3
  • Let me clarify, `a` and `b` are parameters that I don't pass in by myself, they are passed into the function by the framework. I want to add an additional 'type' parameter that I pass in myself. – LeonCS Sep 05 '14 at 13:34
0

Just check for type in the function

"render": function(a, b,type) {
 if(type !== undefined){
      //do stuff with type
   } 
 // do stuff 
 }
andrew
  • 9,313
  • 7
  • 30
  • 61
  • Let me clarify, `a` and `b` are parameters that I don't pass in by myself, they are passed into the function by the framework. I want to add an additional 'type' parameter that I pass in myself. – LeonCS Sep 05 '14 at 13:34