A brief analysis of the difference _javascript skill of function declaration and function expression in JavaScript

Source: Internet
Author: User

There are two ways to declare functions in javascript: function declarations and function expressions.

The difference is as follows:

1. Functions defined by the method declared by the function, the function name is required, and the function expression of the function name is optional.

2. Functions defined by the method declared by the function, functions can be called before the function declaration, and function expression functions can only be called after the declaration.

3. Functions defined by the method of the function declaration are not true declarations, they can only appear in the global, or nested in other functions, but they can not appear in the loop, condition or try/catch/finally, and

A function expression can be declared anywhere.

There are two ways to define a function, respectively:

Copy Code code as follows:

Function-declaration
function greeting () {
Console.log ("Hello World");
}
function expression
var greeting = function () {
Console.log ("Hello World");
}

Here's an interesting javascript:

Copy Code code as follows:

function f () {Console.log (' I am outside! ');}
(function () {
if (false) {
Repeat declaration once function f
function f () {Console.log (' I am inside! ');}
}
f ();
}());

What does it output? The first reaction should be "I am outside". Results in chrome output "I am inside", IE11 direct error, Firefox lower version of the output "I am outside" ...

The results of the chrome output clearly reflect the features of functions declared with function declarations-functions that can be invoked before they are declared.

The IE error shows missing objects because the function declaration is in the condition and violates the principle of the function declaration.

Scope of function Expressions:

If a function expression declares a function that has a function name, then the function name is equivalent to a local variable of this function, which can only be invoked inside the function, lifting a chestnut:

Copy Code code as follows:

var f = function fact (x) {
if (x <= 1)
return 1;
Else
Return X*fact (x-1);
};
Alert (fact ()); Uncaught referenceerror:fact is not defined

Fact () can be invoked inside a function, and an error is invoked outside the function: fact undefined

The above is the entire content of this article, I hope you can enjoy.

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.