PDA

View Full Version : Default values in functions?



djr33
04-19-2013, 01:16 AM
I've had the feeling that I'm becoming more used to programming in JS, though I still don't do it very much. But then today I ran into an odd translation issue from PHP that has left me confused and gave a 'parse error' in several browsers (Chrome, Safari, at least).

In PHP I'd assign a default value to a variable in a function like this:

function example($var=1) {
return $var;
}

In Javascript, I tried to do the same, but apparently that's bad Javascript:

function example(var=1) {
return var;
}

Is there a workaround for this? Since JS has annoying ways to check if a variable isn't set, it's hard to give a default value anyway. For the moment I worked around this in my current project, but I'd like to know what's going on here.

Is there just a different perspective I should adopt when writing JS?

In PHP, it's bad code (will cause a fatal error) to omit any argument, unless it has a default value like that. What about JS? It seems to not mind skipping one.

jscheuer1
04-19-2013, 03:00 AM
Javascript is different, you don't need to define everything, but you can, and if you do, the syntax is also different. And the word var is reserved, so you cannot use it as a variable name. There are several ways to handle it. The simplest is:


function example(myvar) {
myvar = myvar || 1;
return myvar;
}

If myvar is undefined, you will get 1. But you will also get 1 if myvar is 0. So if it's a number you're after and 1 is what you want if it's not a number:


function example(myvar) {
myvar = typeof myvar !== 'number'? 1 : myvar;
return myvar;
}

It's really kind of messy though, to equal PHP, you would do:


function example(myvar) {
myvar = typeof myvar === 'undefined'? 1 : myvar;
return myvar;
}

That way you will get whatever myvar is as long as it's not undefined.

But you almost never see that. Usually it's just assumed that myvar will be whatever's expected, and that if it isn't, the script will break and the author must trace back to where that happened and fix it. Alternatively you can test myvar in various ways, for what it's most likely to be, and as a result of that determine what to do next, that's also pretty common:


function example(myvar) {
if (typeof myvar === 'number') {do something;}
else if (typeof myvar === 'string') {do something else;}
else {do yet another thing;}
}

djr33
04-19-2013, 03:48 AM
And the word var is reserved, so you cannot use it as a variable name.Oops. That's unrelated to my problematic code. That was just a (bad) example for here :)

Some of those ideas are interesting, especially the x||y example. I like that.

It's really kind of messy though, to equal PHP, you would do:Hm, yes, but ok. I'll make a note of that code.


...That way you will get whatever myvar is as long as it's not undefined.

But you almost never see that.What I'm trying to do is create exceptions. My function looks something like this:

function myfunction(myvar,ignoresomething) {
if (ignoresomething!=1) {
alert(myvar);
}
}
In short, what I want to do is skip part of a function if there's some special circumstance, as determined by that second value. (I'll usually in PHP set that by default to 0, meaning proceed like normal, while I'll mark an exception with a 1, so that the function skips one part or another.) Basically I want to preserve most of the behavior of the function, but I want to skip one step.

It seems like it will work most of the time to use != (although PHP would be unhappy about using an undefined variable), but for some reason I'm in the habit of using if (somevar==1) to note WHEN to skip it, rather than WHEN NOT to skip it. No big deal.