Functions in JavaScript

Source: Internet
Author: User

1, JS in the function of the overloaded: JS function, parameters are not required, a function hypothesis defines a few parameters, then the caller can pass, or can not pass, can also be only a two .... can also be transmitted a lot, so we can not rely on the number of parameters to distinguish between two functions of the same name (the code below understanding)

<! DOCTYPE html>functionFA (A, B) {alert (a+b); }        functionfa () {alert (10); } FA (300,600); varFB =function(A, B) {Alert (A +b)}; FB=function() {Alert (10);}; FB (12, 16);//Ten        varFC =NewFunction (A, B, "alert (a+b)")); FC=NewFunction ("alert (10)")); //js function, parameters are not required, a function hypothesis defines a few parameters, then the caller can pass, or can not pass, can also be only a two .... Can also be passed many, so we can not rely on the number of parameters to distinguish between two functions of the same name                //The overloaded simulation of functions in JS:        functionDoadd () {//arguments object: An array of parameters.             if(Arguments.length = = 1) {alert (arguments[0] + 10); }            Else if(Arguments.length = = 2) {alert (arguments[0] + arguments[1]); }        }                //a function is called, and two kinds of results are obtained by the number of arguments passed. Doadd (10);// -Doadd (30, 20);// -</script>


2, JS in the function of the parameter array (arguments) access

<! DOCTYPE html>//arguments parameter array        functionSayhi () {alert ("Hello" + arguments[0] + "," + arguments[1]);//access to parameter arrays} sayhi ("Nicholas", "How is you today?"); functionHowmanyargs () {alert (arguments.length);//length of the parameter array} howmanyargs ("ing", 45);//2Howmanyargs ();//0Howmanyargs (12);//1</script>

Functions in JavaScript

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.