The latest analytic _javascript techniques of JS global variables and local variables

Source: Internet
Author: User

Is this blind principle based on the research test to stun me, it is necessary to do so? Actually understand the principle there is no need to do so many one test, and then come to the law, ECMAScript rules have been defined.

The rule of Var is that declaring a variable with VAR is an internal variable, or it is calling a global variable first, no matter how many layers of functions.

The rule of this is: this is always pointing to itself in the method function, which always points to domwindow.

//GodDamnTest1 function Foo () {var a = 123;//local variable, global variable of all child functions THIS.A = 456;//Object Properties (function 
() {alert (a);//123, Global Alert (THIS.A);//undefined, normal function, this point to Domwindow}) (); 
var f = new Foo (); 
GodDamnTest2 function Foo () {var a = 123; 
THIS.A = 456; (function (a) {//Local declaration alert (a);//456, overridden by a function local declaration of a global}) 
(THIS.A); 
var f = new Foo (); 
GodDamnTest3 function Foo () {var a = 123; 
THIS.A = 456; (function () {alert (a);//123, Global Alert (THIS.A);//undefined, domwindow this.b = 789;//window.b = 789}) 
(); (function () {alert (this.b);//789, window.b}) 
(); 
var f = new Foo (); (function () {alert (this.b);//789, window.b}) 

(); 
GodDamnTest4 function Foo () {(function () {this.b = 789;//window.b = 789}) (); 
(function () {alert (this.b);//789, window.b var b = 0; alert (b);
0, this test is also written out! 
})(); 
var f = new Foo (); (function () {alert (this.b);//789, window.b alert (b);//789, window.b}) (); 

Surprisingly, the last alert (b), the result is still 789. No damn surprise at all!

GodDamnTest5
function Foo () { 
(function () { 
this.b = 789;//window.b = 789
}) (); 
(function () { 
alert (this.b);//789, window.b
alert (b);//Undefined, global
var b = 0; 
alert (b); 0, and this test!
}) (); 
} 
var f = new Foo (); 
(function () { 
alert (this.b);//789, window.b
alert (b);//789, window.b
}) ();

Ps:js method of deleting local variables

Alert (' Value: ' +str+ ' \ttype: ' +typeof (str))////Before declaring variable, reference
var str= "DD";
Alert (' Value: ' +str+ ' \ttype: ' +typeof (str))//Declare and assign a variable, reference the
str=undefined; Delete local variable
alert (' Value: ' +str+ ' \ttype: ' +typeof (str))//Cancel variable, reference, same as first

The above is a small set to introduce the JS global variables and local variables of the latest analysis, I hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.