JavaScript function notes

Source: Internet
Author: User

A function is a block of statements with a specific function. The definition of a function uses the keyword function, with the following syntax:

function funcName ([parameters]) {    statements;    [Return expression;]}

The meaning of each part of the function:

    • FuncName is the function name. The function name can be defined by the developer and is the same as the variable naming convention.
    • Parameters is the parameter of the function. When calling a function, you pass the actual data to the parameter list to complete the function's specific function. Parameter list can be defined in one or more parameters, multiple parameters separated by commas "," of course, parameter list can also be empty. The parameter here is " formal parameter", which is the formal argument.
    • Statements is the function body. The function body specifies the function, which is the body part of the function.
    • Return returns the value of the specified function. A function that can specify the return value with a return statement, or it can have no return value. When the function executes to the return statement, the execution of the function ends, regardless of any subsequent code.

Anonymous functions:

An anonymous function is a function that has no name. An anonymous function can pass a parameter, or it can assign a value to a variable.
An anonymous function, which can be understood as setting a function, but does not assign it to a variable. This is defined in the following way:

function ([parameters]) {    statements;    [Return expression;]}

Use method: You can pass parameters in parentheses.

function Demo () {    //  function body part }

Equivalent to

var demo=function() {    //  Functions body part }

You can call the function like this:demo();

The effect of parentheses is to execute the function body (code block) that the demo variable points to as a function.

Without the parentheses, the body of the function is just a piece of text, a string that does not execute.

The first parenthesis encloses the function body, the second parenthesis executes the function body, and [parameters] is the list of arguments to pass.

(function(x, y) {    alert (+100);}) (10,100);
    • The actual participation definition function passed by the calling function specifies that the formal parameters are in turn corresponding, that is, the value of the 1th argument is passed to the 1th parameter, and the value of the 2nd argument is passed to the 2nd parameter ...
    • An argument that exceeds a shape parameter does not pass its value.
    • If no corresponding argument (the number of arguments is less than the number of formal parameters) is passed in, its value is undefined.

JavaScript function notes

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.