JS (global scope)

Source: Internet
Author: User

Global function scope (put the declaration of the variable and the declaration of the function in front)

Scope: The scope in which a piece of data can be used. In general, the data used in a program code is not always valid/available, and the scope of the code that qualifies the availability of the data is the name. The use of scopes improves the locality of the program logic, enhances the reliability of the program, and reduces the name collisions.

Variable (data) JS, the scope of the variable is two, one is the global scope (global variable), a local scope (local variable), a variable is a global variable or a local variable, mainly see the position of the variable declaration. Declared inside a function, is the local variable of this function.

Global scope---Anywhere access to a variable defined outside of a function that has global scope does not use VAR to define a variable that has global scope all properties on a Window object have global scope not declared within any function that has global scope local scope---can only be accessed within the function Variables defined inside a function using VAR, and functions declared inside a function using function, have local scope

See Scope for details

Output: Zhangsan

Equivalent

2. Execution can also be done before the definition

This and arguments will determine the value before execution

Scope Chain

[[Scopes]]: scope When we declare a function, and the function creates a property that is [Scopes], the variable that we declare in this function is stored in the function's [[Scopes]] property in the lookup rule for variables and functions: When we call a piece of data, JS will first look in the current scope, if not found, to find the scope of the parent, if not found in the scope of the parent, continue to look up until the scope of the window. If you can't find it in the window, it's an error.

JS (global scope)

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.