-3

Let's say I have:

function myFunk(param1) {
    if (param1) {
            // do stuff
    } 
}

and I alter this to ...

function myFunk(param1, param2) {
    if (param2) {
            // do stuff
    }
}

param2 can only ever be true or false. Is this safe?

rockstardev
  • 13,479
  • 39
  • 164
  • 296
  • 2
    What do you mean by "safe" ? – Denys Séguret Jun 26 '13 at 12:53
  • 2
    And what do you mean with "overload" ? You're changing the function, not overloading it. – Denys Séguret Jun 26 '13 at 12:54
  • See [Function overloading in Javascript - Best practices](http://stackoverflow.com/questions/456177/function-overloading-in-javascript-best-practices) – PP. Jun 26 '13 at 12:54
  • 4
    Well that will work but its not overloading its replacing one function with another. – Mark Broadhurst Jun 26 '13 at 12:54
  • Functions can't be overloaded in JavaScript because the arguments are typeless. Also, you may define a function with say 2 args, but you can pass more/less than 2 args and it won't fail. So what you are doing is replacing the same function with a new definition. – mohkhan Jun 26 '13 at 12:55

4 Answers4

2
function myFunk(param1, param2) {
    if (param2) {
            // do stuff
    }
}

param2 can only ever be true or false.

No. It can have any value. If the function is called with only one argument (myFunk("something")), then the param2 will have the value undefined. Which is falsy (like null, "", 0 or NaN) of course, so as expected your if-statement will not be executed.

Is this safe?

It's the standard way to have optional arguments in JavaScript, yes. For further details see Function overloading in Javascript - Best practices

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

In this case, I feel it would be more clear if you use function variable approach.

var myFunk = function(param1) {
    if (param1) {
            // do stuff
    } 
}

and I alter this to ...

myFunk = function(param1, param2) {
    if (param2) {
            // do stuff
    }
}

It is now very clear what is happening, and the previous version of function myFunc does not exist anywhere.

edit more info

If you call function(param1, param2) {} with only one parameter, then typeof param2 == "undefined" will return true. For example:

myFunk = function(param1, param2) {
    if (typeof param2 == "undefined") {
        // do stuff with only param1
    }
}
000
  • 26,951
  • 10
  • 71
  • 101
0

You cannot overload functions in javascript. you would be better checking the arguments or checking for undefined in order to see what the caller is trying to do. for example

function myFunk(param1, param2) {
    if (param2) {
            // do stuff
    }
}

myFunk(1); // if does not execute

myFunk(1, 2); // if executes

however if you pass a falsy value for the second param then the if will still not execute

myFunk(1,0);  // if does not execute
Mark Broadhurst
  • 2,675
  • 23
  • 45
-1

Function overloading is not possible in javascript, if you made two same name function in same scope, always last function with that name will be called irrespective of number of parameters.

Pranav Singh
  • 17,079
  • 30
  • 77
  • 104