Introduction to Functions:
Description: A function is a section of JS code that is defined once but can be called or executed as many times as possible, and functions sometimes have parameters that specify local variables of values when the function is called, and functions often use these parameters to calculate a return value.
function declaration:
Description: The function can encapsulate any number of statements, and can be anywhere, at any time invoke execution, function declaration with the Functions keyword declaration, followed by a set of parameters and function body
function Calcresult () {console.log (' only function is called, I will be executed! ')} Call function-no need to pass parameter Calcresult ()//function Calcresult (prefix) {if (prefix==undefined) {prefix = ') required to be passed: '} Console.log (prefix+ ' only function is called, I will be executed! ')} Call function-Requires Calcresult ("Jean Jean said:")
Note: Functions are not overloaded, so when you declare two functions with the same name and call them, they call the last defined function, even if Calcresult () is written above the function definition
function returns:
Note: A function with parameters and without parameters, which does not define a return value but executes directly after the call, executes directly after the call, and virtually any function can implement the return value with the return statement followed by the value to be returned
function UserInfo (name, age) {return ' My name is ' + name + ', my ages are ' + age}console.log (userInfo (' Li full ', 25))
Note: Once the function internally return the code after the return statement is no longer executed, jump out of the code block and continue backwards from the point of call
Function parameters:
Description: The JS function does not mind passing in the number of parameters, nor because the parameters are not uniform and error, in fact, the function body can use the arguments object to accept the arguments passed in
function sum () {var sum = 0//Arguments object gets the parameter object for (var i=0; i<arguments.length; i++) {sum + = Argume Nts[i]} return sum}//call function Console.log (SUM (1, 2, 3, 4, 5, 6, 7, 8, 9))
This article is from the "ζ Automated operation and maintenance development Road ζ" blog, please be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1846125
Website Frontend _javascript.0007.javascript function related