JavaScript immediately executes function __ Block chain

Source: Internet
Author: User
Tags function definition

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, 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: Functions fnname () {...}; Use the function keyword to declare a function, and then specify a function name, called a function declaration.

Functional 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 that the JavaScript engine, when parsing JavaScript code, will ' function declaration elevation ' (function declaration hoisting) The declaration of functions on the current execution environment (scope). The function expression must wait until the JAVASCIRTP engine executes to its line to parse the function expression from the top and the next line, two, the function expression can be immediately called with parentheses, and the function declaration can not be called only in FnName () Form. Here are two examples of the difference between the two. Help

1 2 3 4 5 6 7 8 9 10 11 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 after the function expression

Help

1 2 3 4 5 6 7 8 9 10 11 12-13 var fnname=
Related Article

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.