func(string1,url,flag,icon), and then call it func(a,b) in another asp. So what are the values of flag and icon, and how to define the default value? Thanks!
--The default value should be undefined
Preset numbers in the function can be used with arguments[i]
i is the position of your parameter first is 0
So if you want to set the default value of flag, you can write it like this
function func(string1,url,flag,icon){ if(!arguments[2]) flag = "123"; if(!arguments[3]) icon = "456"; }
You try it, it should be
I encountered a problem today. I needed to call a JS function and wanted to give it a default parameter in the function, thinking it was the same as other languages.
<script> function test(id=0){ alert(id); } </script> <input type="button" value="test" onclick="test()">
The operation result is an error. The default parameters cannot be transmitted in JS like this. I checked it online. You can use arguments to implement the parameter group. Please refer to the following example:
<script> function test(a){ var b=arguments[1]?arguments[1]:50 return a+':'+b } alert(test(5)) alert(test(5,9)) </script>
A little difference from other languages. .
--var b=arguments[1]?arguments[1]:50 You can also write it as: var b=arguments[1] || 50;
I especially like its feature.
--var b= arguments[1] || 50; This method is quite simple.
The above article briefly talks about the default parameters of function in js is all the content I share with you. I hope you can give you a reference and I hope you support me more.