On function scope and declaration of JS in advance

Source: Internet
Author: User

JavaScript functions have two main features, one is the function is an object, the other is the function provides scope support, and today the main chat function scope support.

Simply look at two concepts:

named function expression

  

var add = function Add (a) {         return a        }

This code describes a function called the ' named function expression '.

If you omit a name from a function expression, it is an ' anonymous function expression ', which is an anonymous function, such as

var add = function (A, b) {    return a + b;};

Finally look at the ' function declaration '

function foo () {    //body}

A function declaration can only appear in "program code", that is, in another function body or globally. This definition cannot be assigned to a variable or property, nor can it be used as a parameter to a function call.

Global scope

function foo () {}

Local scope

function local () {    function bar () {}    return bar;}

At this point the bar is within the local, that is, outside the access, called the local scope, so we see this code

var a = 1;function aa () {     console.log (a);  }

At this point the result of the output is 1, well understood, because inside the function is not accessible, so will go outside to find, until found

Add a line of code, and then look at

var a = 1;function () {   console.log (a);   var a =2;  }

At this point the output is underfined; it is surprising that there is no definition before the function can be accessed outside the functions, the definition is not accessible, what is the actual situation,JS defined inside the function of the variable will be promoted to the top of the function body, but only the definition, not assigned value, so the above code is like this

var a = 1;function () {   var A;     Console.log (a);   var a =2;  }

Since a is defined but not assigned, it certainly underfined after the console.

Now that the function declaration is lifted, will the function expression be, look at the following example

Global functions function foo () {    alert (' Global foo ');} function Bar () {    alert (' Global Bar ');} function Hoistme () {    console.log (typeof foo);     Console.log (typeof bar);     Foo ();     Bar ();     function foo () {        alert (' local foo ');    }    var bar = function () {        alert (' local bar ');}    ;} Hoistme ();

After the last code, this time it is easier to understand that the function inside will overwrite the function defined outside, but what is the result?

    Console.log (typeof foo);  "Function"    Console.log (typeof bar);//"Undefined"    foo ();//"local foo"    bar ();//Typeerror:bar is not a function

Isn't it a little bit surprising that it's easy to figure it out

As I said earlier, the function declaration is ahead of time, so the variable foo and its definition implementations are advanced, and the function definition implementation is independent of the writing order, and this is demonstrated by the intentional writing in the back.

But the function expression does not have this skill, so the variable is advanced, but the definition is not in advance.

Thank you for watching

On function scope and declaration of JS in advance

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.