Adding default values ​​to javascript custom function

mmorning, im php man, so I cannot understand why this is causing the 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 is "remove_wrap = false" from the function.

can someone tell me what i should write here. hi phil

+4
source share
7 answers
 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
source

JavaScript does not support argument arguments like this. Like many others, you can choose a default inside your function. A brief idiom for setting the default value:

 arg = arg || default; 

So in your case it will be:

 remove_wrap = remove_wrap || false; 

However, if your default value is false , you do not even need to set it. Since when this argument is not enough, its value will be of type undefined inside the function, and this value is already "false": 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: Please note that I also added var to the error variable (don't omit var if your true intention is to create a global variable). I also shortened the assignment with a triple expression. I also reformatted a fancybox call with a syntax that most people find more readable.

+4
source

Because you are a PHP person, this can come in handy. Based on JS myself, I actually mixed up a couple of times, assuming it 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 value is true, otherwise "false".

But in JavaScript, the "or" operator actually returns the first value, if true. It returns only false if both values ​​are false. If both values ​​are false, then it returns the second value. This means that the above is a super efficient way to assign default values.

My sincere apologies if all this was old knowledge. I just want someone to go through me when I switch languages.

+3
source

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

For instance:

 function display_message( bool, msg, remove_wrap ) { if(undefined === remove_wrap) remove_wrap = false; //Rest of function } 

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

+1
source

You can handle the default values, for example:

 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:

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

Comparing with null convenient because it allows the caller to explain the default value, but it is not prone to overriding 0 or '' (empty string) as a simple comparison of falsifications.

+1
source

to try

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

You cannot declare default values ​​in the function declaration parameters section. Instead, to declare default values, your function declaration should look like this:

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

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

+1
source

Source: https://habr.com/ru/post/1337422/


All Articles