Small discovery of Var a=b=c=d in JavaScript

Source: Internet
Author: User

Read someone else's blog and jot it down

Let's take a look at the following code

1 var a=1,b=2,c=3; 2     3     (function() {4         var a=b=1; 5     })(); 6 7     Console.log (a); 8     Console.log (b);

This is a well-known closure, yes, a very important use of closures is that variables can not be accessed externally, will not pollute the external variables , so you would think that the results of 7,8 rows are 1 2? Otherwise, 7, 8 rows of the correct result is 1, 1.

Why is that? I also think the result is 1 2, in fact, JS is executed, in the closure of VAR declaration is a local variable, so a is a local variable, and B, B isnot declared with Var, so in the closure of B is a global variable , so the front of the b=2 is closed inside the b=1 covered, So the outputs A and B are all 1 .

That's how I understand it. We have different understanding can leave a message, because the first time to write a blog, poor writing, we forgive

Small discovery of Var a=b=c=d in JavaScript

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.