6 function output See the block-level scope of JS

Source: Internet
Author: User

1.

var output = 0;(function() {    output+ +;} ()); Console.log (output);

The function operates on global output because JS does not have a block-level scope, so output is 1.

2.

var output = 0;(function(output) {    output+ +;} (output)); Console.log (output);

The function passes in the output value, but the function only adds an operation to the parameter output and does not operate on the global output, so output is 0.

3.

var output = 0;(function() {    output+ +;} (output)); Console.log (output);

The output value within the function is not used by the function itself, and is not passed through the parameter, so the global output is added with an output of 1.

4.

var output = 0;(function() {//A    var output = 0;    (function() {//B        output++;    } ());} ()); Console.log (output);

The output of function B is an operation that operates on output in the A function, has no effect on the global output, and output is 0.

5.

var output = 0;(function() {//A    (function() {/ / B        output++;     }());     var output = 0;} ()); Console.log (output);

Output in B cannot get global output, so the output plus one operation is not valid. So the output is 0.

6.

var output = 0;(function() {//A    (function() {/ / B        output++;    } (output));} ()); Console.log (output);

Performs an output plus operation in the global, so the output is 1.

6 function output See the block-level scope of 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.