(function () {}) ();, xx=xx| | {}; the definition and role!

Source: Internet
Author: User

Recent projects have encountered this problem, by the way record!

Find the information on the Internet to understand understanding, give my understanding as follows:

Question 1:

(function () {}) (): This function () {} represents an anonymous one, and () wraps the functions () {}, () representing the highest priority execution, which takes precedence over the function () {}. The last () means that the function () { } is called immediately after executing function () {}.

Like what:

If function () {} is compared to function xx () {}, () represents XX (), the defined XX function is called.

Case:

(function () {
Alert (0)
})();

Result: the frame shows 0;

(function () {
Alert (0)
});

Results: no box display 0;

Question 2:

xx=xx| | {};//means declaring a command space called XX (Defining a global variable--in JavaScript, variables that are not declared with VAR are treated as global variables ).

xx.kk= (function () {

Xx.yy=function () {///Declare a namespace called XX.YY (the object in the closure is passed to the global variable through the addition of the global variable, which results in a code encapsulation

return "XXX";

};

var data1= "";

(function () {

data1= "XXX";

})();

return {DATA1:DATA1};

})();

-----------Call

Alert (Xx.yy ());

Or

alert (XX.KK.DATA1);

(function () {}) ();, xx=xx| | {}; the definition and role!

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.