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!