JavaScript---Scopes

Source: Internet
Author: User

  Scope

    What is a scope?

Scopes are the accessibility of variables, functions, and objects when your code is running . in other words, the scope determines whether the variables in your code can be referenced by the outside world.

Why do I need scopes?

Minimum access principle. Scopes provide a level of security for your code, with different scopes interfering with each other. Facilitates tracking of bugs and avoids conflicting names of variables.

What are the scopes?

Global scope (window)

Local scope (function)

Block-level scope (ES6 let/const keyword)

The scope in JS refers to the lexical scope. A section of JS code execution takes 2 stages:

1. Compile stage--the compiler compiles your code (lexical), and at this time, your scope is determined.

2. Execution phase--js engine starts to execute your code

The scope of JS is often confused with the execution context. How do you differentiate it?

The scope is determined at compile time, and the execution context is created when the function is executed (the execution context will be addressed later, note that the global scope can be considered a special ' execution context ', and the window is first understood as a function bar)

JavaScript---Scopes

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.