The function functions setting parameters in JS have a default value

Source: Internet
Author: User

I can write this in PHP

The code is as follows Copy Code

function ABCCCC ($a =1, $b =2) {
return $a + $b;
}

Calls can be directly

The code is as follows Copy Code

ABCCC ()
ABCCC (1)
ABCCC (1,1)

But in JS this write hint hint variable undefined undefined, but I think the default value in JS should be undefined

Preset numbers in functions can be used arguments[i]

I'm the location of your argument, the first one is 0.

So to set the default value of flag, you can write this

The code is as follows Copy Code

function func (String1,url,flag,icon) {
if (!arguments[2]) flag = "123";
if (!arguments[3]) icon = "456";
}

As follows

  code is as follows copy code

<script Type= "Text/javascript"

Function Test (a) {
var b = arguments[1]? arguments[1]:;
return a + ': ' + b;
}
Alert (Test (5));
Alert (test 5,9);

</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.