1. In JavaScript, the format of the function
function name (formal parameter list) { functional body;}
2. JavaScript, the function needs to pay attention to the details
1. In JavaScript, when a function defines a parameter, it is not possible to declare a variable using the var keyword.
2. In JavaScript, there is no return value type, and if the function needs to be returned to the caller, it returns directly (return) and does not return if it does not need to be returned.
3. In JavaScript, there is no overloaded concept, and the function defined later overrides the function defined earlier.
4. In JavaScript, an object of arguments (array) is implicitly maintained inside any function, passing the value of the function to the arguments object and then assigning the data to the parameter by the arguments object.
3. Example
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <MetaCharSet= "Utf-8">5 <title>Exercises for functions in JavaScript</title>6 </Head>7 <Scripttype= "Text/javascript">8 //Requirement: Define a function to do the addition operation9 functionAdd (A, b) {Ten varC=a+b; One document.write (c+"<br/>");//return Value: 33//After opening the following function, there is no output value, it has been overwritten by the following function A returnC; - } - //var d=add (11,22,13,34,14,15); the //document.write (d+ "<br/>");//return value: - - Add ( One, A, -, the, -, the) - functionAdd (a,b,c) { + varD=a+b+C; - document.write (d+"<br/>");//return Value: + document.write (arguments.length);//return Value: 6//Get internally maintained array arguments (array) length A } at </Script> - <Body> - </Body> - </HTML>
Example result diagram
|
Original Dshore
Author's homepage:http://www.cnblogs.com/dshore123/
Source:https://www.cnblogs.com/dshore123/p/9447785.html
Welcome reprint, reprint must explain the source. ( If this article is helpful to you, you can click on the lower right corner of the recommendation , or comments, thank you!) ) |
Java Basics functions in JavaScript (Web knowledge)