On the variable scope of JavaScript

Source: Internet
Author: User

1, the variable follows the first declaration and then use.

Console.log (b); b=123;

Code Run Result:

Uncaught REFERENCEERROR:B is not defined

2. The local variables defined inside the method cannot be accessed externally.

function my () {var a= ' Hi ';} My (); Console.log (a);

  

Code Run Result:

Uncaught REFERENCEERROR:B is not defined

The same is the error that is not defined by B.

3, one method nested another method, the outside method can access the variables declared inside; (closures implement object-oriented)

function my () {var c=123;function f2 () {console.log (c);} return F2;} var result=my (); result ();

Results: 123

The F2 method calls my c local variable, and the My method returns F2 to invoke the F2 method externally;

4, add var defined local variables, not add to the global variable (no scope problem exists)

On the variable scope of 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.