This article will show you how to set default values for function parameters in Javascript. For more information, see
In php, I can write it like this.
| The Code is as follows: |
Copy code |
|
Function abcccc ($ a = 1, $ B = 2 ){
Return $ a + $ B;
} |
Direct call
| The Code is as follows: |
Copy code |
|
Abccc ()
Abccc (1)
Abccc (1, 1) |
In js, the following code prompts that the variable is undefined, but I think the default value in js is undefined.
You can use arguments [I]
I indicates that the first parameter is 0.
So you can set the default flag value.
| The Code is as follows: |
Copy code |
|
Function func (string1, url, flag, icon ){
If (! Arguments [2]) flags = "123 ";
If (! Arguments [3]) icon = "456 ";
} |
As follows:
| The Code is as follows: |
Copy code |
|
<Script type = "text/javascript">
Function Test (){
Var B = arguments [1]? Arguments [1]: 50;
Return a + ':' + B;
}
Alert (Test (5 ));
Alert (Test (5, 9 ));
</Script> |