JS (function () {...}) () Execute function writing now to understand the data structure of __

Source: Internet
Author: User
Tags function definition

Go from: http://my.oschina.net/u/2331760/blog/468672?p={{currentpage+1}}

JavaScript is 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, and later found that the reason for parentheses is not so. To understand the immediate execution function, you need to first understand the basic concepts of some functions.


function declarations, function expressions, anonymous functions

function declaration :
function FnName () {...};
Use the function keyword to declare a function, and then specify a function name, called a function declaration.

function Expression:
var fnname = function () {...};
Using the function keyword to declare a function, but not naming the function, and finally assigning an anonymous function to a variable called a function expression, is the most common form of function expression syntax.

anonymous function :
function () {};
Declaring a function with the function keyword, but not naming it, is called an anonymous function, an anonymous function is a function expression, and an anonymous function has many functions, giving a variable to create a function, assigning an event to an event handler or creating a closure, and so on.

the difference between a function declaration and a function expression is:
First, the JavaScript engine will ' function declaration elevation ' (function declaration hoisting) The functions declared on the current execution environment (scope) when parsing JavaScript code. The function expression must wait until the JAVASCIRTP engine executes its line to parse the function expression from the top and the next line.
Second, the function expression can be immediately called with parentheses to call the function, the function declaration can not be called only in the form of FnName (). Here are two examples of the difference between the two.

Instance:
?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21-22 FnName (); function FnName () {...}//normal, because the function declaration of ' ascension ' can be fnname before the function declaration ();  var fnname= function () {...}//error, variable fnname has not yet saved a reference to the function, the function call must be var fnname= function () {alert (' Hello World "); }();

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.