Valid JavaScript Variable Scope Item 8-9 Globals and Locals, scopeglobals

Source: Internet
Author: User
Tags variable scope

Valid JavaScript Variable Scope Item 8-9 Globals and Locals, scopeglobals

This series serves as the Reading Notes for objective JavaScript.

 

Item 8: fewer global objects

 

Important:

  1. Global Objects can bring convenience, but experienced programmers can avoid them. Because it brings potential risks of name conflicts
  2. Global variables are the bond between different modules. global variables can only be used between modules to access functions provided by each other.
  3. Do not use global variables when using local variables.
  4. In browser, this keyword will point to the global window object
  5. Two methods are used to change the global object. The var keyword is used to declare and set attributes for the Global Object (through the this keyword)
  6. The Feature Detection of the current running environment is detected through the Global Object. For example, if (this. JSON) to determine whether the current running environment supports JSON

 

Summary:

  1. Avoid declaring global variables
  2. Use local variables whenever possible
  3. Avoid adding attributes to Global Objects
  4. Use global variables for Feature Detection

 

Item 9: always declare local variables

 

Important:

  1. Implicit global variables are more difficult than global variables. For example:

function swap(a, i, j) {    temp= a[i]; // global    a[i]= a[j];    a[j]= temp;}

The above temp is implicitly declared as a global variable.

  1. Use the lint tool to check whether global variables are implicitly declared in JavaScript code.

 

Summary:

  1. Always remember to declare local variables with the var keyword
  2. Use the lint tool to ensure that no global variables are implicitly declared.

 

 

 

 


In python, why do the parameters in eval (source [, globals [, locals]) Follow '[' or ']'?

The parameter with [] is optional.


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.