JavaScript anonymous function Summary __ function

Source: Internet
Author: User
Tags anonymous

Previous blog about JavaScript anonymous functions: http://blog.csdn.net/wusuopuBUPT/article/details/14522631


first, enumerate several common ways to declare JavaScript functions:

Function Demo () {
    //Functions body part
}

It is the essence of the function of the principal assignment to the demo variable, the function of the main body is:
function () {
    //Functions body part
}

So the demo () function can also be defined as this:
var demo=function () {
    //function body part
}

You can call a function like this:
Demo ();

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


Without parentheses, the body of the function is only a piece of text, a string of strings that will not be executed.

Give me a chestnut:

<script>
	var total = 1;
	var demo = function () {Total
		= ten;
		return total;
	}
	
	Alert (demo); Output function Body
	alert (total);//1
	Alert (demo ());//alert (total
	);
</script>


Anonymous Functions

An anonymous function, which can be understood to have a function defined, but does not assign it to a variable.

The definition is as follows:

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


Call to anonymous function:


The anonymous function does not have a function name and does not have a variable that points to it, and cannot be invoked anywhere, like a named function, which must be executed immediately after the definition.

As mentioned above, the function of the parentheses is to execute the code block as a function and to pass the arguments within the parentheses, so the anonymous function can be invoked and executed using parentheses:

(function ([parameters]) {
    //function body part
}) ([parameters]);

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


Example 1:
(function () {
    alert ("anonymous function");
}) ();

Example 2:
(function (x,y) {
    alert (x+y);
}) (1,2);

Note: End with semicolon "; ", it's not a code block, it's called a function."

Example 3: Computes the value of 1+2+3+...+99+100 with an anonymous function.

<script language= "JavaScript" type= "Text/javascript" >
(Function (m,n) {
    var total=0;
    if (m>=n) {
        alert ("value range is not correct. ");
        return false;
    }
    for (Var i=m;i<=n;i++) {
        total+=i
    }
    Alert (total);
}) (1,100);
</script>

Save and execute code, pop-up warning box, show 5050, anonymous function executed successfully.


Example 4: anonymous function with return value
<script language= "JavaScript" type= "Text/javascript" >
alert (
    function (m,n) {
        var total=0;
        if (m>=n) {
            alert ("value range is not correct. ");
            return false;
        }
        for (Var i=m;i<=n;i++) {
            total+=i
        }
        return total;
    }) (1,100)  Do not add a semicolon here
);
</script>


Save and execute the code with the same result as in Example 3.


Summary: The function name is a reference to the body of the function (the code block), which is performed as a function of the code block.

Reference: http://www.itxueyuan.org/view/6314.html

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.