Functions are mainly used to encapsulate specific function code.
Functions are declared in such a way that the keyword function, the function name, a set of parameters, and the pending code that is placed in parentheses.
Format:
Function name (formal parameter list) {
function body;
}
JavaScript functions to be aware of the details:
1. In JavaScript, when a function defines a parameter, a variable cannot be declared with the Var keyword, and the parameter name can be written directly.
2. A function in JavaScript does not have a return value type, and if the function needs to return data to the caller, it is returned directly, if no return is required.
3. In JavaScript there is no concept of function overloading, and a function with the same name as defined above directly overrides the previously defined function.
4. An arguments (array) object is implicitly maintained inside any function within JavaScript, and is passed to the arguments object when the data is passed to the function.
The data is then assigned to the parameter by the arguments object.
code example
1 <HTMLxmlns= "http://www.w3.org/1999/xhtml">2 <Head>3 <Script>4 5 functionShowday () {6 //locate the corresponding Label object. 7 varInputobj=document.getElementById ("Month");8 //get input Tag data9 varMonth=Inputobj.value;Ten /* One if (month==1| | month==3| | month==5| | month==7| | month==8| |month==10| | MONTH==12) { A alert ("This month is 31 days"); - }else if (month==4| | month==6| | month==9| | month==11) { - alert ("This month is 30 days"); the }else if (month==2) { - alert ("This month is 28 days"); - }else{ - alert ("No such month"); + } - */ + A Month=parseint (month); at Switch(month) { - Case 1: - Case 3: - Case 5: - Case 7: - Case 8: in Case Ten: - Case A: to Alert ("this month is 31 days"); + Break; - Case 4: the Case 6: * Case 9: $ Case One:Panax Notoginseng Alert ("this month is 30 days"); - Break; the Case 2: + Alert ("this month is 28 days"); A Break; the default: + Alert ("no that month"); - Break; $ $ } - - the } - Wuyi the </Script> - Wu - <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /> About <title>Untitled Document</title> $ </Head> - <Body> -Month:<inputID= "Month"type= "text" /><inputtype= "button"value= "Query"onclick= "Showday ()" /> - A </Body> + </HTML>
Note: Data obtained from a Web page is of type string.
JavaScript (quad)----functions