Explore the similarities and differences of three ways of declaring global variables in JavaScript

Source: Internet
Author: User
      This article mainly introduces the similarities and differences of three ways of declaring global variables in JavaScript. Variable and variable declarations are the most basic concepts of a language, which beginners will soon master. Need friends can come over the reference, I hope to be helpful to you variable and variable declaration is a language the most basic concept, beginners will soon master. This is also true for declaring variables in JavaScript, very simple var (keyword) + variable name (identifier).   Mode 1   VAR test; var test = 5; Note that the sentence cannot be contained within a function, otherwise it is a local variable. This is the first way to declare a global variable.   Mode 2   test = 5; Without using Var, assign a value directly to the identifier test, which implicitly declares the global variable test. Even if the statement is within a function, test becomes a global variable when the function is executed.   Mode 3   window.test; Window.test = 5; This approach is often used when an anonymous function is executed to expose some functions to the global. such as the last sentence in JQuery1.5   window.jquery = window.$ = JQuery;   If you just use variable test, then three ways will make no difference. For example, alert (test) will show 5. But there are three different ways to be different in some cases. Declare three variable a1,a2,a3 in the above three ways respectively.   A1 = 11; var a2 = 22; window.a3 = 33;   1,for in Windows   for (A in window) { if (a== ' A1 ' | | a== ' A2 ' | | a== ' A3 ') {  alert (a) &nbsp}} IE6/7/8/9: Only the A3 is ejected, stating that the global variable declared through the first, two ways will not be obtained through the for in window. The firefox/chrome/safari/opera:a1,a2,a3 are all popped up, stating that global variables declared in three ways are available through the for in window.     2,delete   try { alert (delete a1);} catch (E) {alert (' cannot delete A1 ')}   try{ alert (delete a2);} catch (E) {alert (' cannot delete A2 ')}   try{ alert (delete A3);} catch (E) {alert (' cannot delete A3 ')}   can see that 1,delete A2 all browsers are false. That is, variables declared through Var cannot be deleted, and all browsers behave in a consistent way. This is also mentioned in the Rhino book.   2, global variables declared through the Window.a3 method cannot be deleted in Ie6/7/8, but in Ie9/firefox/chrome/safari/opera.     Although there are two different points, it returns true when in operation.   Alert (' A1 ' in Windows);//true alert (' A2 ' in Windows);//true alert (' A3 ' in window);//true use with To open object window closures All browsers are also consistent, as follows   with (window) { if (A1) {  Alert (A1);//11  }  if (A2) {  Alert (A2);//22 &NBSP}  if (A3) {  alert (A3);//33  } }

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.