Js setting the default value of function parameter of is suitable for the case of no arguments
- 2020-03-30 02:06:29
- OfStack
Today, I had a problem writing a div+ CSS library that simulates a js info box. I wanted to automatically use "prompt" as the window title when I didn't pass any arguments. Let me try to write it like this
There is no doubt that I failed (otherwise I would not have posted this post)
Finally after some baidu, found such a good thing
From what I can tell, arguments are something like an array, starting at 0 and representing arguments to the function in order
For example, arguments[0] in the above example represents the argument a
In fact, the arguments [0]? Arguments [0]:50 can also be written: arguments[0] || 50; It's pretty neat. That's how js sets the default value of the function parameter
function MessageBox(title=""){
}
There is no doubt that I failed (otherwise I would not have posted this post)
Finally after some baidu, found such a good thing
function test(a){
var b=arguments[0]?arguments[0]:50;
return a+':'+b;
}
From what I can tell, arguments are something like an array, starting at 0 and representing arguments to the function in order
For example, arguments[0] in the above example represents the argument a
In fact, the arguments [0]? Arguments [0]:50 can also be written: arguments[0] || 50; It's pretty neat. That's how js sets the default value of the function parameter