JavaScript Basics-Instant functions (Immediate Functions)

Source: Internet
Author: User

1. Declaring methods for immediate functions

The immediate function (Immediate Functions) is a special JavaScript syntax that allows the function to execute immediately after it is defined:
(function () {
Alert (' Watch out! ');
}()); Here are a few of the following to understand this notation:
    1. The orange part is a function expression;
    2. The blue pair of parentheses represents the immediate execution, and the parentheses are the parameters required to execute the function (this example does not require parameters);
    3. Wrap up the upper part with a pair of parentheses (which is the black pair).
Black This pair of brackets can make people understand that this expression is the return value of the function, not the function object. Many people prefer the following wording, which is exactly the same as above:
(function () {
Alert (' Watch out! ');
})();
2. Application scenarios for instant functions
When you need to execute some initial code at the start of the program, the initialization is done only once, and the temporary variables used will no longer be useful, so you can put the code in an immediate function to execute:
(function () {    var days = [' Sun ', ' Mon ', ' Tue ', ' Wed ', ' Thu ', ' Fri ', ' Sat '],        today = new Date (),        msg = ' Today Is ' + days[today.getday ()] + ', ' + today.getdate ();    Alert (msg);} ()); "Today is Fri, 13"

3. Parameters of the immediate function
The following example shows a real-time function with parameters:

prints://I met Joe Black on Fri 23:26:59 GMT-0800 (PST) (function (who, when) {    Console.log ("I met" + Who + ' on ' + when);} ("Joe Black", New Date ()));

Sometimes, you can also pass the global this object to the immediate function, so that the code in the immediate function can be reused in a different runtime environment:

(function (global) {    //access the Global object via ' global '} (this);

Of course, if the parameters of the immediate function are more, and the function body is longer, you need to look up and down the function's parameter list definition and its formal parameter list, which is sometimes annoying.
4. Return value of immediate function
Just like the normal function, the immediate function also has a return value, which assigns the return value to the variable (note that this is not the assignment of a function object to a variable):

var result = (function () {    return 2 + 2;} ());

If you remove the brackets outside the immediate function, the effect is exactly the same. This syntax is more concise, but it is easy to assume that the function object is assigned to the variable, in fact, the variable is the return value of the function:

var result = function () {    return 2 + 2;} ();

As mentioned above, the immediate function is written in two ways, so the following code is identical in effect to the two above:
var result = (function () {
return 2 + 2;
})();
These three examples return the original data type. In fact, immediate functions can return any data, including function objects:

var GetResult = (function () {    var res = 2 + 2;    return function () {        return res;    };} ());

Because the immediate function can provide a separate local scope and finally a return value, some people prefer to use immediate functions when assigning an initial value to an object's properties:

var o = {    message: (function () {        var who = ' me ', what            = ' call ';        return what + "" + Who;    ()),    getmsg:function () {        return this.message;    }};/ /usageo.getmsg (); "Call me" o.message;  "Call Me"

In the above example, the message is a string-type property, not a method.
5. Use of instant functions
Instant functions provide local variable scopes, do not pollute the global environment, and the code can be executed immediately, so it is often used in modular programs. If we put each function independent code snippet to organize with the immediate function, and put in a separate JS file, then when we want to use, the JS file can be introduced into it:

Module1 defined in Module1.js (function () {  //all the Module 1 code ...} ());

http://zihui.lin.blog.163.com/blog/static/7292115420126335225160/

JavaScript Basics-Instant functions (Immediate Functions)

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.