Examples of Self-executed anonymous functions in avascript and avascript

Source: Internet
Author: User

Examples of Self-executed anonymous functions in avascript and avascript

Self-executed anonymous functions in Javascript
Format:

(Function () {// code })();

Explanation: this is a pretty elegant code (it may be confusing if you see it for the first time :)). The first pair of parentheses in the function () {}) returns the Untitled function to the script, then a pair of empty parentheses immediately execute the returned untitled function, which contains the parameters of the anonymous function.
Here is an example with parameters:

(Function (arg) {alert (arg + 100) ;}) (20); // in this example, 120 is returned.

Important purpose: You can use it to create a namespace. As long as you write all your code in this special function package, the outside will not be accessible unless you allow

(function(){ function $(id){ return document.getElementById(id); } function __addClass(id,className,classValue){ $(id).style.className=classValue; } window['mySpace']={}; window['mySpace']['addClass']=__addClass; })();

In the preceding example, you can use this pseudo namespace to encapsulate and protect all your functions, objects, and variables. Besides, because they are in the same function, they can be referenced from each other. To render the protected code, a pair of parentheses tell the browser to immediately execute the returned anonymous function, and assign _ addClass () to a window method during execution, in this way, only addClass can be executed externally, while _ addClass is protected. I can call it like this: mySpace. addClass ('oneid', 'font-width', 'bold ')


Small issues with self-execution of javascript anonymous Functions

The second function is not executed anonymously,
The first one is an anonymous function (but this is not recommended because it may cause ambiguity). Whether a is a function or num... is actually a num.

Second missing function name Error
The third is anonymous function execution (recommended)

The main reason is the browser's Parsing Method for js. If you have to write code similar to 1, you need to study how the browser parses js, whether to start from right to left or from left to right, there are a lot of judgments ....

Therefore, as a user, we need to write Standard Code. This type of ambiguous Code cannot be written,

Why does javascript use anonymous functions or anonymous closures?

1. Define a private scope to avoid data pollution.
2. After the execution is complete, destroy the instance to avoid long memory.
Var Calculator = function (eq ){
Var eqCtl = document. getElementById (eq );
Return {
Add: function (x, y ){}
};
};
This function is followed by (function (){

} (); The difference is that the returned add referenced function points to the internally defined function (x, y), and its scope includes the external function (eq ), since add is global, garbage collection knows that there are references, so these functions will not be destroyed until the window environment does not exist, that is, the browser is closed to release the memory. while the anonymous one is executed once, and there is no external reference in itself, garbage collection will be executed ~! This is the biggest difference between the two.
Anonymous functions are used to simulate block-level scopes to avoid data pollution,

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.