In JS, the usage of (function () {}) () is resolved

Source: Internet
Author: User

Often used, summed up today, the following digest from a Netizen's summary:

(function ($) {...}) (JQuery) is actually an anonymous function that does not know that a friend can continue to look down.

This is actually anonymous function functions (ARG) {...} This defines an anonymous function that, when called with Arg, is written with parentheses and arguments after the function, and because of the precedence of the operator, the function itself needs to be enclosed in parentheses, namely: (function (ARG) {...}) (param) This is equivalent to defining an anonymous function with a parameter of ARG, and calling this anonymous function as a parameter (function ($) {...}) param (jquery) is the same, the reason why only use $ in formal parameters is to not conflict with other libraries, so the argument is using jQuery var fn = function ($) {...}; FN (jQuery);

(Funtion () {}) (); Execute the function immediately; it is equivalent to declaring a function, calling it directly after the declaration, and if the parameters are as follows:
(Funtion (str) {alert (str)}) ("Output")) ; equivalent: funtion outputfun (str) {alert (str);};o Utputfun ("Output");

Recently in the finishing

Javascript

Learn to find out about this problem

And found an explanation on the Internet.

The most clear

Chu

Most understand

(function () {}) ()

Equivalent to defining first

function xx () {}

, after calling

XX ();

()

is the highest priority, so first execute the

function () {}

This defines an anonymous function that is equal to

Xx=function () {}

And then it's called

XX ()

The

;

Give an example

JScript Code

Functionf1 (a)

{

alert (a);

}

Functionf2 (a)

{

Returnfunction () {

alert (a);

}

}

Here's

var x=f2

is equivalent to passing the function to the

F2,

And then we're going to execute this function

,

It must be added

()

That is

X ();

That is

F2 ()

Still is

(function f2 (a) {

return function () {

alert (a);

})();

There's another problem.

Is the problem with namespaces

YY = yy| | {};---

Declare a call

Yy

Namespaces (defining a global variable)

(function () {

Yy. Lang=function ()

{------

Declare a call

Yy. Lang

The namespace

(through the global variable

Add attributes to the object in the closure to the global variable, to achieve the effect of code encapsulation)

Isundefined:function (o) {

return typeof o = = = ' undefined ';

},

Isstring:function (o) {

return typeof o = = = ' String ';

}

};

})();

---

Calling methods

Alert (YY. Lang.isstring (' Test Me ');

It was not clear at this time,

Why did you add it upstairs?

(Function ()

{

And

)();

The most out of these two lines,

What's the use?


J

Script Code

yy=yy| | {};

//---

Declare a call

Yy

Namespaces (defining a global variable)

//(

function ()

{

Yy. Lang=function ()

{

//------

Declare a call

Yy. Lang

The namespace

(By increasing the properties of the global variable, the object in the closure is passed

To a global variable to achieve the effect of code encapsulation)

Isundefined:function (o)

{returntypeofo=== ' Undefi

Ned ';

},

Isstring:function (o)

{returntypeofo=== ' string ';

}

};

}

//)();

//---

Calling methods

Alert (YY. Lang.isstring (' Test

Me '));

Questions in this:??????

Comment out what's different about these two lines? No, it's a statement, why use it?

() () () execute a little bit?

For:

(

function () {}) ()

It means to execute the preceding parenthesis immediately.

function

, which is equivalent to an anonymous function

;

Because the inside

The execution of the Code,

Defines the

Yy. Lang

This object,

So you can execute

Alert (YY. Lang.isstring (' Test

Me '));

Call

Isstring

Method.

However, if you write the following

Functionaa () {..};/ /

It just defines a name

A function of AA, but did not execute


Alert (YY. Lang.isstring (' Test

Me '));//

It's not going to be found when it's executed.

Isstring

The method.

What is the advantage of writing this???????????????

(function{}) ()

There is a good way to use it.

Javascript
The visible range of variables is the execution script

Space-saving time. For example, the following notation

1 and 2 to achieve the same purpose, but the notation 1 is due to a variable

The visible range is only in the anonymous function body,

So a

After executing this anonymous function, the space is freed.

But the notation 2

Will always exist on this page.

Writing

1

Jscript

Code

(function () {vara=2;

alert (a);

})();

Writing

2

Jscript

Code

vara=2;

alert (a);

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.