4

mmorning, im a php man so I cant understand why this is throwing an error:

function display_message( bool, msg, remove_wrap = false ) {
    if( bool === true ) {
        error = get_msg( msg, remove_wrap );
    }else{
        error = get_error_msg( msg, remove_wrap );
    }
    $.fancybox( error, { 'autoDimensions' : false, 'width' : 450, 'height' : 'auto', 'transitionIn' : 'none', 'transitionOut' : 'none' });
}

the culprit being 'remove_wrap = false' from the function.

can anyone tell me what i should be writing here. regards, phil

7 Answers 7

4

JavaScript doesn't support argument defaults like that. Like many other have said, you can pick a default inside your function. A concise idiom for setting a default value is:

arg = arg || default;

So, in your case it would be:

remove_wrap = remove_wrap || false;

However, if your default value is false, you don't even need to set it. Because, when that argument is lacking, its value will be of type undefined inside the function and that value is already "falsy": you can directly use remove_wrap as it is:

function display_message(bool, msg, remove_wrap) {
    var error = bool
        ? get_msg(msg, remove_wrap)
        : get_error_msg(msg, remove_wrap);

    $.fancybox(error, {
        autoDimensions: false,
        width: 450,
        height: "auto",
        transitionIn: "none",
        transitionOut: "none"
    });
}

Bonus: Note that I've also added a var to the error variable (don't omit the var unless your true intention is to create a global variable). I've also shortened the assignment with a ternary expression. I've also reformatted the fancybox call with a syntax that most people find more readable.

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

Comments

3

Because you're a PHP man, this might come in handy. Coming from JS myself, I actually messed up a couple of times, assuming this was in the language:

function (some_var) {
    some_var = some_var || 'default';
}

In PHP, the "or" operator will return "true" if the first value or second values are true, otherwise "false."

But in JavaScript, the "or" operator actually returns the first value itself, if it's truthy. It only returns false if both values are falsy. If both values are falsy, then it returns the second value. That means that the above is a super efficient way of assigning defaults.

My sincerest apologies if this was all old knowledge. I just wish someone had walked me through when I was switching languages.

Comments

2
function display_message( bool, msg, remove_wrap ) {
    remove_wrap = remove_wrap === true;
    if( bool === true ) {
        error = get_msg( msg, remove_wrap );
    }else{
        error = get_error_msg( msg, remove_wrap );
    }
    $.fancybox( error, { 'autoDimensions' : false, 'width' : 450, 'height' : 'auto', 'transitionIn' : 'none', 'transitionOut' : 'none' });
}

2 Comments

This is pretty much the solution /i came to after the first post (without the example) so theremore i must accept as answer, thanks for everyone who game an answer all do the job.
Second line could be reduced to remove_wrap = remove_wrap === true;
1

You cannot supply default values to javascript function parameters, but you can leave them undefined and then assign a default at the beginning of your function.

For example:

function display_message( bool, msg, remove_wrap ) { 
    if(undefined === remove_wrap) 
        remove_wrap = false;

    //Rest of function
}

Call this function like this: display_message(true, "my msg");

Comments

1

try

function display_message( bool, msg, remove_wrap) {
  if(remove_wrap === undefined) {
    var remove_wrap = false;
  }
...

Comments

1

You can't declare default values within the parameter section of your function declaration. Instead, to declare defaults your function declaration should look like this:

function display_message( bool, msg, remove_wrap ) {
  remove_wrap = remove_wrap || false;
  // rest of code
}

Thus, if remove_wrap is defined it will stay equal to itself, but if it is undefined it will be set to false.

Comments

1

You can handle default values like this:

function display_message(bool, msg, remove_wrap) {
    if (remove_wrap == null) remove_wrap = false;
    // rest of your code...

}

The default value will be applied if the function is called with either:

display_message(some_bool, some_msg);
// Or,
display_message(some_bool, some_msg, null);

Comparing to null is handy because it allows the caller to explicity ask for the default value, but it isn't prone to overriding 0 or '' (empty string) like a simple falsy comparison would.

Comments

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.