Share common questions about javascript scopes and javascript questions

Source: Internet
Author: User

Share common questions about javascript scopes and javascript questions

This article will share with you the relevant content about the javascript scope interview questions for your reference and study. Let's take a look at it.

I. Scope:

Before understanding the scope, you must first understand some basic concepts:

Every variable or function has its own scope. It is out of scope and cannot be used.

2. Global variables and local variables:

1. Global variables:

(1) variables declared globally, suchvar a=1;

(2) Only values with no declarations are assigned, sucha=2;(Note: If a = 2 is in the function environment, it is also a global variable)

2. Local variables:

A variable written into a function is called a local variable.

3. role:

(1) program security.

(2) Release of memory.

Iii. Scope chain:

The process of finding the quantity. First, check whether your local environment has declaration or function. If so, check whether the declaration has a value assignment or function content. If not, search for it at the upper level.

Iv. Pre-resolution sequence:

Every program has to do the work. The program starts to pre-parse the syntax, check whether the punctuation marks are correct, check whether the memory can be accommodated, and parse the variables ...... It is not until the resolution is correct that it starts to follow the normal process order. Imagine how bad the performance would be if the pre-resolution order is not followed by the Process Order. Maybe the program runs to the last function and the syntax error is found!

Ordered content:

1. The <script> block referenced in the file is parsed in sequence and connected from top to bottom.

2. var in each script block (Note: Only the variable name is parsed and the value is not parsed, as shown in figurevar a=2;Resolve var a to the beginning of the environment, and do not parse the subsequent values. Only when the program runsvar a=2In this case, the variable is assigned a value.) The function is parsed to the beginning of the block.

3. parse each environment in sequencevar,functionIt is parsed to the beginning of the environment.

5. application scenarios (some common scope-related Interview Questions ):

Var a = "aa"; function test () {alert (a); // undefined. After the function is executed, var a is pre-parsed in the function environment, when a is displayed, first check whether there are any declarations in the environment of the current layer. However, the Code does not execute a value assignment, so the result is undefined. Var a = "bb"; // var a is pre-parsed at the beginning of the function, and alert (a) is assigned only when this line is executed; // "bb"} test (); alert (a); // "aa" finds the declaration in the global environment and finds var a = "aa"
Var a = "aa"; function test () {alert (a); // "aa", after the function is executed, in the function environment, the declaration about a in the current stage was not found, so we started to look up the previous stage. A = "bb"; // The number of global a changes after this line is executed} test (); alert (); // "bb" Global Environment a has been changed during function execution
Function test () {B (); // function B is pre-parsed, so it can be called and output 1 is executed; var a = 1; function B () {console. log (1); console. log (a); // undefined var a = 2 ;}} test ();

Vi. Summary:

To understand the scope of a variable, the focus is to clarify the pre-resolution sequence and then determine the scope of the variable. There are routines: First, check whether the current layer environment has any declarations. If yes, check whether a value is assigned. Only the Declaration does not execute the value assignment, that is, undefined. If there is no declaration or value assignment, you can search for it again until it is found. If no execution environment is found, the console reports an error where the variable cannot be found.

Functions are simpler: Find whether there are pre-parsed functions in the current layer environment, and execute them if there are pre-parsed functions. If not, search up.

Well, the above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, you can leave a message, thank you for your support.

Related Article

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.