(Function () {}) () Usage

Source: Internet
Author: User

I recently learned about JavaScript and found this problem. I found this explanation on the Internet to be the clearest and clearest;
(Function (){})()
It is equivalent to defining function XX () {} first, and then calling XX (); () is the highest priority. Therefore, executing function () {} first defines an anonymous function, xx = function () {} is followed by calling XX (). Here is an example.
JScript code

function f1(a){                alert(a);              }  function f2(a) {            return function(){                            alert(a);                        }            }                     

 


VaR x = F2 here is equivalent to passing the function to F2. to execute this function, you must add (), that is, x (); that is, F2 ()
Or

(function f2(a){                 return function(){                           alert(a);    })();  

 


Another problem is the namespace.

YY = YY | {}; --- declare a namespace named YY (defining a global variable) (function () {yy. lang = function () {------ declare a name called yy. lang namespace (upload the objects in the closure to the global variables by adding attributes to the global variables to implement code encapsulation) isundefined: function (o) {return typeof o === 'undefined';}, isstring: function (o) {return typeof o === 'string ';}};})(); --- Call method alert (yy. lang. isstring ('test me '));

 

 
At this time, some people do not know why to add (function () {and) () upstairs; what is the use of the two outermost rows?

JScript code

YY = YY | {}; // --- declare a namespace named YY (defining a global variable) // (function () {yy. lang = function () {// ------ declare a name called yy. lang namespace (upload the objects in the closure to the global variables by adding attributes to the global variables to implement code encapsulation) isundefined: function (o) {returntypeofo = 'undefined';}, isstring: function (o) {returntypeofo = 'string ';}};}//)(); // --- Call method alert (yy. lang. isstring ('test me '));

 


The question is :?????? What are the differences between the two lines? Isn't it a statement? Why do I need to execute?
A:
(Function () {}) () means to immediately execute the function in the previous bracket, which is equivalent to an anonymous function. Because of the Code Execution, YY is defined. lang object, so you can execute alert (yy. lang. isstring ('test me'); call the isstring method.
However
Functionaa () {...}; // only defines a function named AA, but it is not executed.
Alert (yy. Lang. isstring ('test me'); // The isstring method cannot be found during execution.
What are the benefits of such writing ??????????????
(Function {}) () has the advantage of using JavaScript variables to save space when executing scripts. For example, the purpose of writing 1 is the same as that of Writing 2, but the visible range of variable A is only in the anonymous function, therefore, a releases space after executing this anonymous function. However, Statement 2 will always exist on this page.
Statement 1:
JScript code

(function(){vara=2; alert(a); })();

 


Statement 2:
JScript code

vara=2; alert(a);

 

 



 

(Function () {}) () Usage

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.