JS closed functions, closures, built-in objects

Source: Internet
Author: User
Tags variable scope

One, variable scope

Variable scope refers to the scope of the variable, the variables in JavaScript are divided into global variables and local variables

1. Global variables: variables defined outside the function are common to the entire page and can be accessed inside and outside the function.

2. Local variables: variables defined inside a function can only be accessed within the function that defines the variable, and cannot be accessed externally. When accessing a variable inside a function, first look inside for the variable, if there is, use internal, if not, go outside to find

second, closed functionA closed function is another way of writing an anonymous function in JavaScript, creating a function that executes at the outset without naming it. 1. Definition and execution function of general function2. Definition and execution of closed functions: (function () {...}) You can also add "~" or "!" before the function definition Symbol to define an anonymous function.

three, closed package

Closures are function nesting functions, and intrinsic functions can reference parameters and variables of external functions, and parameters and variables are not reclaimed by the garbage collection mechanism.

1, closure purposes 1: A variable is stationed in memory for a long time, can be used in the loop to save the index value

2, Closure purposes 2: Private variable counter, external inaccessible, to avoid pollution of global variables

Iv. built-in objects1. Document

For example, when viewing the Product Details page, want to buy, this will jump to the login page, after the successful login to use this method to jump to the Product details page

document.referrer//get the address of the previous Jump page (requires server environment)

2. location

A, window.location.href//Get or reset URL address

  

B, Window.location.search//Get Address Parameters section

  

C, Window.location.hash//Get page anchor point or hash value

  

3. Math

A, math.random get a random value of 0-1

  

B, Math.floor down rounding

  

C, Math.ceil Upward rounding

JS closed functions, closures, built-in objects

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.