Parse JS global variables and local variables

Source: Internet
Author: User
Is this blind research and testing without a theoretical basis confusing me, is it necessary to do so? In fact, if you understand the principle, there is no need to do so many tests one by one, and then draw a rule, ECMAScript rules have been defined. the var rule is: use var to declare that the variable is an internal variable; otherwise, the global variable is called first, no matter how many layers of functions. the rule of this is: th in the method function is such a blind and unprincipled research and testing that has caused me to faint. Is it necessary to do so? As a matter of fact, there is no need to do so many tests one by one after understanding the principles, and then draw a rule. The ECMAScript rules have been defined.

The var rule is: use var to declare that the variable is an internal variable; otherwise, the global variable is called first, no matter how many layers of functions.

The rule for this is: this in the method function always points to itself, and this in a common function always points to DOMWindow.

// GodDamnTest1function Foo () {var a = 123; // local variable, global variable of all sub-functions this. a = 456; // Object Property (function () {alert (a); // 123, global alert (this. a); // undefined, common function, this points to DOMWindow}) () ;}var f = new Foo (); // GodDamnTest2function Foo () {var a = 123; this. a = 456; (function (a) {// local declaration alert (a); // 456, which is overwritten by a declared locally by the function}) (this. a);} var f = new Foo (); // GodDamnTest3function 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}) (); // GodDamnTest4function Foo () {(function () {this. B = 789; // window. B = 789}) (); (function () {alert (this. b); // 789, window. bvar B = 0; alert (B); // 0. This test is also written. !}) ();} Var f = new Foo (); (function () {alert (this. b); // 789, window. balert (B); // 789, window. B })();


Surprisingly, the last alert (B) still returns 789. // No damn surprise at all!

// GodDamnTest5function Foo () {(function () {this. B = 789; // window. B = 789}) (); (function () {alert (this. b); // 789, window. balert (B); // undefined, global var B = 0; alert (B); // 0, and this test !}) ();} Var f = new Foo (); (function () {alert (this. b); // 789, window. balert (B); // 789, window. B })();

PS: JS method for deleting local variables

Alert ('value: '+ str +' \ ttype: '+ typeof (str) // reference var str = "dd"; alert ('value: '+ str +' \ ttype: '+ typeof (str) // after declaring and assigning a variable, reference str = undefined; // Delete the local variable alert ('value: '+ str +' \ ttype: '+ typeof (str) // After the variable is canceled, the reference is the same as the first one.



The above is the details of parsing JS global variables and local variables. For more information, see other related articles in the first PHP community!

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.