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

May 16, 2016 pm 04:58 PM
function parameter default value

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.
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

New feature in PHP version 5.4: How to use callable type hint parameters to accept callable functions or methods New feature in PHP version 5.4: How to use callable type hint parameters to accept callable functions or methods Jul 29, 2023 pm 09:19 PM

New feature in PHP version 5.4: How to use callable type hint parameters to accept callable functions or methods

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

What does function mean?

i9-12900H parameter evaluation list i9-12900H parameter evaluation list Feb 23, 2024 am 09:25 AM

i9-12900H parameter evaluation list

What do product parameters mean? What do product parameters mean? Jul 05, 2023 am 11:13 AM

What do product parameters mean?

C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument Sep 17, 2023 am 10:49 AM

C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument

PHP Warning: Solution to in_array() expects parameter PHP Warning: Solution to in_array() expects parameter Jun 22, 2023 pm 11:52 PM

PHP Warning: Solution to in_array() expects parameter

C++ function parameter type safety check C++ function parameter type safety check Apr 19, 2024 pm 12:00 PM

C++ function parameter type safety check

Summary of machine learning hyperparameter tuning (PySpark ML) Summary of machine learning hyperparameter tuning (PySpark ML) Apr 08, 2023 pm 07:21 PM

Summary of machine learning hyperparameter tuning (PySpark ML)

See all articles