Home > Web Front-end > JS Tutorial > js sets the default value of function parameters (suitable for cases where no parameters are passed)_javascript skills

js sets the default value of function parameters (suitable for cases where no parameters are passed)_javascript skills

WBOY
Release: 2016-05-16 16:58:28
Original
1182 people have browsed it

Today I encountered a problem when writing a div css class library that simulates a js information box. I hope that when I don't pass parameters, "prompt information" will be automatically used as the window title. I tried to write like this

Copy code The code is as follows:

function MessageBox(title="" ){
}

There is no doubt that my operation failed (otherwise I wouldn’t have posted this blog post)

Finally after some Baidu, I found this Good stuff
Copy code The code is as follows:

function test(a){
var b=arguments[0]?arguments[0]:50;
return a ':' b;
}

According to my humble understanding, arguments are probably similar to An array, with subscripts starting from 0, represents the parameters of the function in order

. For example, arguments[0] in the above example represents parameter a

. In fact, arguments[0] ?arguments[0]:50 can also be written as: arguments[0] || 50; The writing method is quite concise. The above is the method to set the default value of function parameter in js.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template