Website Frontend _javascript.0007.javascript function related

Source: Internet
Author: User

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

Related Article

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.