JS Execute function Writing immediately understand the data structure

Source: Internet
Author: User
Tags closure function definition types of functions

JavaScript is quite casual compared to other programming languages, so the JavaScript code is full of all kinds of exotic work, and sometimes, of course, understanding the various types of writing is a further insight into JavaScript language features.

(function () {...}) () and (function () {...} ()) is a common way for two kinds of JavaScript to execute functions immediately, initially I thought it was a bracket wrapped anonymous function, followed by parentheses to call the function, finally reached the function definition immediately after the purpose of execution, later found that the meaning of parentheses is not so. To understand the immediate execution function, you need to first understand some of the other basic concepts of the function.


function declarations, function expressions, anonymous functions

function declaration: Functions fnname () {...}; First the function keyword, and then the function name, called the Declaration of functions.

anonymous function: function () {}; There is only one function keyword that defines a functions, does not declare a function name, so it is called an anonymous function, and the anonymous function is actually a function declaration, which requires a special syntax call function, so the anonymous function has many functions, giving a variable to create a function, Assigning an event becomes an event handler or creates a closure, and so on.

Functional expression var fnname = function () {...}; Assigning an anonymous function to a variable to create a function, called a function expression, is the most common form of functional expression syntax.

The difference between a function declaration and a function expression is that the JavaScript engine prioritizes the function declaration when parsing the JavaScript code, and the function expression JAVASCIRTP engine resolves only from the top and the next line. Here are two examples of the difference between the two.

FnName ();
function FnName () {
	...
}
Normally, because the function is resolved, the function call can be fnname () before the function declaration

;
var fnname=function () {
	...
}
Error: Function not declared, function call must be after function expression
var fnname=function () {
	alert (' Hello world ');
} ();
The function expression is followed by parentheses, which can be invoked immediately when the JavaScript engine resolves to the function
FnName () {
	alert (' Hello world ');
} ();
Syntax error, function declaration cannot call function in parentheses after function body, can only be called as FnName ()

After you understand some basic concepts of functions, look back (function () {...}) () and (function () {...} () These two types of functions are immediately executed, initially I thought it was a bracket wrapped anonymous function, and followed by parentheses call the function immediately, the original is not so, to the function body after parentheses can be called immediately, then the function body must be a function expression, cannot be a function declaration .

(function (a) {
	console.log (a);   Firebug output 123, using () operator
}) (123);

(function (a) {
	console.log (a);   Firebug output 1234, using () operator
} (1234);

! function (a) {
	console.log (a);   Firebug output 12345, use. Operator
} (12345);

+function (a) {
	console.log (a);   Firebug output 123456, using the + operator
} (123456);

-function (a) {
	console.log (a);   Firebug output 1234567, using the-operator
} (1234567);

var fn=function (a) {
	console.log (a);   Firebug output 12345678, using the = operator
} (12345678)

function (a) {
	console.log (a)    //Syntax error, anonymous functions, or function declarations, cannot be invoked after parentheses.
} (m);              Specifying an operator before a function causes the statement to be valid and converts the function declaration to a function expression.

You can see the output, plus in front of the function. , +,-even commas wait until they can be executed immediately after the function definition, and (). , + 、-、 = and other operators convert function declarations to function expressions , eliminating ambiguity between function expressions and function declarations, telling the JavaScript engine that this is a function expression, not a function declaration, that can be done immediately after the function body is defined with parentheses.

Parenthesis is the safest procedure, because. , +,-and so on, can also be calculated with the return value of the function, sometimes causing unnecessary trouble.

But what is the use of this writing?

JavaScript does not use the concept of a private scope, which can mimic a private scope, create a closure with an anonymous function, and call the function immediately to execute code, and the internally defined variable does not conflict with the external variables, which is what jquery uses. Wrap jquery code in (function (window,undefined) {... jquery code ...} (window), the jquery internal variable does not conflict with the external variable.

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.