JavaScript function declarations and function expressions

Source: Internet
Author: User

Functions in JavaScript are different from other languages, and each function is maintained and run as an object. By the nature of the function object, it is convenient to assign a function to a variable or to pass the function as a parameter. Before continuing, take a look at the use syntax of the function:

The code is as follows Copy Code

function Func1 (...) {...}

var func2=function (...) {...};

var func3=function func4 (...) {...};

var func5=new Function ();


There is also a custom function expression that is used primarily to create a new scope in which variables declared within the scope

Do not conflict with or confuse variables in other scopes, mostly in anonymous functions, and are automatically executed immediately:

The code is as follows Copy Code

(function () {

var x = ...

})();

This kind of function expression is categorized as the second of the above two methods and is also considered as a function expression.

Both method one and method two create a function that is named Foo, but there is a difference.

There is a mechanism in the JAVASCRIPT interpreter for a variable declaration to be promoted (hoisting), meaning that the declaration of a variable (function) is elevated to the front of the scope, even if the code is written at the end, or it is elevated to the front.

For example, the following code snippet:

The code is as follows Copy Code


Alert (foo); function foo () {}

alert (bar); Undefined

function foo () {}

var bar = function Bar_fn () {};

Alert (foo); function foo () {}

alert (bar); function Bar_fn () {}

The output results are function foo () {}, undefined, function foo () {}, and function Bar_fn () {}.

You can see that Foo's declaration is written after alert and can still be called correctly because JavaScript explains

The device will elevate it to the front of alert, and the function bar created with the function expression does not enjoy this treatment.

So whether bar has been promoted, in fact, the variables declared with VAR will be promoted, but is to be first assigned

For undefined, so the second alert pops up undefined.

So the sequence of the JavaScript engine executing the above code might be this:

Create variable foo and bar, and assign them all to undefined.

Create the function body of the function foo and assign it to variable foo.

Executes the previous two alert.

Create a function bar_fn and assign it to bar.

Executes the following two alert.

Note:

Strictly speaking, there is another way to create a function in JavaScript, called the function constructor:

The code is as follows Copy Code

var foo = Function (' Alert (' hi! ');


This method forms a function body with a string as a parameter. But in this way, the efficiency of execution will be compromised,

The code is as follows Copy Code

var funcname=new Function (P1,p2,,pn,body);

The type of the parameter is a string, p1 to the list of parameter names for the function created by the PN, and the body represents the function statement of the function being created, FuncName is the name of the function being created. You can create an empty function without specifying any parameters, and do not specify funcname to create a nameless function, of course, that function has no meaning.

Note that P1 to PN is a list of parameter names, that is, P1 not only represents an argument, it can also be a comma-separated list of arguments, such as the following definition is equivalent:

The code is as follows Copy Code

New Function ("A", "B", "C", "Return A+b+c")

New Function ("A, B, C", "Return A+b+c")

New Function ("A,b", "C", "Return A+b+c")

The syntax for JavaScript to introduce a function type and provide a new function () is because the function object adds properties and methods that must rely on the type of function.

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.