I have the following javascript code:
function toggleHiddenOps(dir, tour = false){
// do stuff
}
I noticed it was breaking in Chrome and IE (seems to work in FF 27). If I change the above to:
function toggleHiddenOps(dir, tour){
// do stuff
}
It resolves the problem. Is there some issue with declaring variables like this in JS? Or perhaps my problem actually lies elsewhere?
Thanks.
-
1Why are you wanting to assign a variable in the arguments, rather than in the function body itself?David Thomas– David Thomas2014年03月02日 15:32:23 +00:00Commented Mar 2, 2014 at 15:32
-
To define a default value. This is possible in PHP.Inigo– Inigo2014年03月02日 15:33:23 +00:00Commented Mar 2, 2014 at 15:33
-
It's possible to define defaults in JavaScript as well, but you have to do it in the function body (for most browsers, but I'm unsure as to why it's preferable to it the way you outline in your question).David Thomas– David Thomas2014年03月02日 15:34:27 +00:00Commented Mar 2, 2014 at 15:34
-
That syntax is coming in ECMAScript6. At the moment, very few engines support it.T.J. Crowder– T.J. Crowder2014年03月02日 15:39:20 +00:00Commented Mar 2, 2014 at 15:39
-
It's a case of writing more succinct code. I often use this syntax in PHP.Inigo– Inigo2014年03月02日 15:40:28 +00:00Commented Mar 2, 2014 at 15:40
4 Answers 4
This functionality exists only in FF for now. It will be in ECMAScript6, so expect it to start showing up in other engines over time.
Chrome, IE, Opera and Safari don't support that syntax in their latest versions.
A good workaround for this is to use arg= typeof arg !== 'undefined' ? arg : defaultValue;
For example:
function multiply(a, b) {
b = typeof b !== 'undefined' ? b : 1;
return a*b;
}
multiply(5); // 5
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/default_parameters
1 Comment
This is illegal in javascript.
If you want a default exactly false:
function toggleHiddenOps(dir, tour){
tour = tour === void(0) ? false : tour;
// do stuff
}
If you only care if its falsey:
function toggleHiddenOps(dir, tour){
// do stuff, tour equals undefined now if not passed 2nd param
}
Comments
Javascript function don't support default paramter, a hack for it is:
function toggleHiddenOps(dir, tour){
tour = typeof tour === 'undefined' ? 'default value' : tour
console.log(dir)
console.log(tour)
}
toggleHiddenOps('dir')
Comments
You cannot specify default values in JS using PHP syntax.
To be fair, draft ECMAScript 6 specification allows to specify default argument value like in PHP, and this is even supported in Firefox 15+, but note that support of ES6 itself is mostly experimental and can theoretically be changed or removed at any point.
A future-proof/cross-browser way to check in JS if argument is not specified is following:
function toggleHiddenOps(dir, tour) {
if ('undefined' === typeof tour) {
tour = 'Some default value';
}
// do stuff
}
8 Comments
undefined. It may be that it's undefined because it was unspecified; alternately, it may be that it was specified, but undefined.arguments.length will tell you. (arguments being a pseudo-array created for every function call containing the actual arguments it was called with.) The check you're doing just checks for undefined, not whether the argument was passed, as I said originally. For supplying a default, I would use what you've put in the code, because arguments can be expensive on some engines. It's just the text saying what the code given does is wrong.