About JS Scopes

Source: Internet
Author: User

We know that in programming languages, scopes are about controlling variables, the visible range of parameters, and the life cycle.

JS provides function scope functions, such as variables defined in the function outside of the variable is inaccessible:

function Jsfunc () {       var a = 5;   // undefined

But JS does not provide the function of block scope, such as we in the Java code if the following code:

     Public Static false ;      Public Static void Main (string[] args) {        if(!  b) {            int a = 5;        }         // compile-time error is reported here    }    

When printing output A, the variable does not define an exception.

And in JS, write the following code:

var false ; function Jsfunc () {
alert (a); Undefined if(! FLG) { var a = 5; } Alert (a// 5}jsfunc ();
alert (a); Uncaught referenceerror:a is not defined

When it pops up 5, it shows that variables defined within the block-level elements within the function can be accessed within the scope of the entire function.

Therefore, we need to pay extra attention when naming variables to avoid the problem of parameter punch.

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.