admin管理员组

文章数量:1326011

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

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

Share asked Jan 28, 2011 at 5:08 Phil JacksonPhil Jackson 10.3k23 gold badges98 silver badges131 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 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.

Because you're a PHP man, this might e 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.

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' });
}

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");

try

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

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.

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 parison would.

本文标签: adding default values within javascript custom functionStack Overflow