Variable name promotion and function name promotion in JS

Source: Internet
Author: User

First, there is no block-level scope for variables in JS, but there is a function scope, that is, only the function can be the scope of the approximate variable.

And, the essence of the function is also a variable, so you can change its value, that is, assign value. So the variable name promotion is very similar to the function name promotion.

1. The promotion of the variable name occurs inside the function. Look at the following example. Description: The first because of the popup undefined, equivalent to the definition of Var num, because inside the function, the definition of Var num=20, is equivalent to the beginning of the definition of Var num; this is the promotion of the variable name.

    var num = ten;     function func () {        //undefined        var num = +;         //  -     }    func ();     // Ten

Equivalent:

var num = ten;     function func () {        var  num;         // undefined        var num =;         //  -     }    func ();     // Ten

If there is no Var

var num = ten;     function func () {        //        num = +;         //  -     }    func ();     //  -

Functions are nested in the same sense

 varnum = 10; functionfunc () {alert (num);//undefinednum = 20; functionfunc1 () {alert (num);//undefined                varnum = 40; Alert (num)// -} func1 () alert (num); // -} func (); Alert (num)//Ten

2. Function name promotion occurs on a lambda function, which is an anonymous function. and variable names to promote a truth.

        var function () {            alert ("abc")        };         function func1 () {            //func is not a function            varfunction () {                alert ("123")            };            Func ()        }        func1 ();

Eliminate Var

  var function () {            alert ("abc")        };         function func1 () {            //ABC            function  () {                alert ("123") )            };            Func ()//123        }        func1 ();

Change the function without using anonymous functions

        var function () {            alert ("abc")        };         function func1 () {            //123            function  func () {                alert ("123") )            };            Func ()//123        }        func1 ();

Variable name promotion and function name promotion in JS

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.