Scope and closure in javascript _ javascript skills

Source: Internet
Author: User
This article mainly introduces the scopes and closures in javascript and uses javascript closures to implement cyclic binding events. If you are interested, refer to them. I. JavaScript Scope

JavaScript variables have only two scopes: global variables and internal variables of functions. The scope of a variable (var scope) defined in any part of a function is the entire function body.
Global variable: refers to the object attributes under the window object.
Scope Division: it is based on context and divided by functions, rather than blocks.
Emphasize two points:
1. In the same scope, JavaScript allows repeated definitions of variables, and the latter will overwrite the previous definition.
2. If the variable defined by the keyword var is not added to the function, the global variable is used by default.

var scope="global"; function t(){   console.log(scope); //"global"   scope="local"   console.log(scope); //"local" } t(); console.log(scope); //"local" var scope="global"; function t(){   console.log(scope); //"undefined"   var scope="local"   console.log(scope); //"local" } t(); console.log(scope); //"global" 

In the variable parsing process, first find the local scope, and then find the upper scope. The scope variable is not defined in the function of the first code, so the upper-level scope (global scope) is searched and its value is output. However, the variable scope is defined in the function of the second code (whether after the console or before the variable is defined, it is considered that the scope has the variable scope ), therefore, the scope of the upper layer is no longer searched, and the scope is directly output. Unfortunately, at this time, the local variable I is not assigned a value, so the output is undefined.

// According to the function scope, you can rewrite the code in the second paragraph as follows: var scope = "global"; function t () {var scope; console. log (scope); scope = "local" console. log (scope) ;}t ();

Due to the function scope feature, local variables are always defined throughout the function body. We can declare variables "in advance" to the top of the function body.

Var B; // Step 2 function fun () {B = "change";} alert (B); // output undefined. Because step 2 defines only var B with no value assigned; // Step 2 function fun () {B = "change";} fun (); // call the above function alert (B); // output change

When a variable is declared using var, the property created cannot be configured, that is, it cannot be deleted using the delete operator.
II. Scope instances

    
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.