JavaScript base uses as few global variables as possible (001)

Source: Internet
Author: User

Pattern means patterns, good coding habits will form patterns after many practical applications, and anti-patterns (anti-pattern) are bad coding habits. Before you get to the JavaScript pattern, take a look at Anti-pattern's example. JavaScript uses functions to manage the scope (scope) of variables. Variables defined inside the function body are local variables, while other variables defined outside the function body are global variables. Global variables are often not used in JavaScript. Such as:

Myglobal = "Hello"; Antipattern, because this is a global variable console.log (myglobal); "Hello" Console.log (Window.myglobal); "Hello" Console.log (window["Myglobal"]); "Hello" Console.log (This.myglobal); "Hello"

There are many problems with global variables. Because global variables are shared by all JavaScript code in the page, it is easy to cause conflicting variables. For example, when you use some 3rd-party JavaScript plugins, such as Jquery,yui,extjs, or a small piece of JS code written by your colleague, if there are some global variables in the code that conflict with the global variables in your program, it can cause some unexpected occurrences.
But JavaScript is a very flexible language, and sometimes you inadvertently let your variables "become" Global:

function sum (x, y) {    //antipattern:implied Global    result = x + y;    return result;}

In the above code, result appears for the first time in the SUM function, but because the fame does not use var, it is a implied global, implicitly known as a global variable. So each time a reputation variable, we should all use the var keyword, so that we can understand where the variable is defined, and also ensure that it is not implicitly known as a global variable:

function sum (x, y) {     var result = x + y;     return result;}

In addition, we should avoid the use of continuous fame, in the following example, a is a local variable, and B is global (this may be different from the average person's perception).

Antipattern, do not usefunction foo () {      var a = b = 0;  Equal to var a = (b = 0); make B "become" global variable      //...}

So we should do this:

function foo () {      var a, b;      // ...      A = b = 0; Both local}

Finally, implicit global variables (not known as VAR) and explicit global variables (known as VAR) are still different in JavaScript. An implicit global variable is actually a property of a global object (an object that comes with the object,javascript), which can be manually deleted, and explicit cannot be deleted. Look at the following example:

Define three Globalsvar Global_var = 1; Explicit Global Global_novar = 2; Antipattern, implicit global (function () {global_fromfunc = 3;//Antipattern, Implicit Global} ());//attempt to deletedelete Global_var; Falsedelete Global_novar; Truedelete Global_fromfunc; true//test the deletiontypeof Global_var; "Number" typeof Global_novar; "Undefined" typeof Global_fromfunc; "Undefined"

By the way, how to access the variable object (Global object) in javascript:

var global = (function () {      return this;} ());

JavaScript base uses as few global variables as possible (001)

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.