Several problems of the JS scope

Source: Internet
Author: User

According to the authoritative guide, the global scope of variables is globally, in the JS code, he has a definition everywhere. A variable declared within a function is defined only in the body of the function. The parameters of the function are also local variables, and they are defined only in the body of the function. Inside a function body, a local variable has a higher precedence than a global variable with the same name. If the name of the parameter declaration of a local variable or function is the same as the name of the global variable, the global variable is effectively hidden.

JS has no block-level scope, and all variables declared in the function, regardless of where, for, if, etc., are defined throughout the function. This is also called variable elevation, scope is in the function.

1, the role of the object inside

?
1 2 3 4 5 6 var a =  10; var AAA = function (){     console.log(this.a); } var b  = {a:2,bbb:AAA};b.bbb();

The last output is the object that 2,this points to. This needs to be kept in mind that he is the scope of his own object. The scope before the point is B, so BBB () executes the scope of B.

2, object sharing issues

?
1 2 3 4 5 6 7 8 var cat ={}; cat.mouse = function(){var b="1111"}; var dog = cat; console.log(dog); console.log(dog.mouse); dog.mouse = function(){var b="2222"}; dog.hat="b"; console.log(cat)

  

function"B"function () {var b= "2222"}__proto__: Object  function () {var b= "1111"function, hat: "B""b"function () {var b= "2222"}__proto__: Object

object is assigned a value, pointing to the same object, changing the second object in a timely manner, and the first object also changing. Conosle.log See is the final result. Referencing the same object, everyone changes together.

3,

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 var test=function(){     var a=1;     setTimeout(function(){         console.log(a);         a=2;     },1000);     a=3;     setTimeout(function(){         console.log(a);         a=4;     },2000);  }; test(); 

The result is 3, 2;

Shared memory. SetTimeout, such as asynchronous, is taking the value of a at that time. When the first settimeout was executed, A=3 was executed. The global object, the same reference. External variables can not only be accessed, but can also be modified.

4,

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 var foo=10; var a=1; varmain = function (){ //a=10; console.log(a); a=20; console.log(this.foo); this.foo=foo; console.log(this.foo); foo=1000; console.log("111111111111"); } var s=main(); var d=new main();

The result is:

1

10

10

111111111111

20

Undefined

1000

111111111111

No new is global, this refers to the global variable. So the first one gets the global value.

The second add new, This.foo point to himself, no definition so the report undefined. Outside a foo is a global variable, main (), after execution, A has become 20, Foo also becomes 1000, so the value has changed because of global variables.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 var foo=10; var a=1; varmain = function (){ //a=10; console.log(a); a=20; console.log(this.foo); this.foo=foo; console.log(this.foo); foo=1000; console.log("111111111111"); } //var s=main(); var d=new main(); 

If you do not perform the first one, the result changes. You can see that the changes are actually global variables.

1

Undefined

10

111111111111

5.

?
1 2 3 4 5 6 7 8 9 10 11 12 var a = 10; var b = function(){     console.log(this.a); } var c = function(){     this.a = 123;     var d = b;     d(); }var e = new c;<br>console.log(window.a);
?
1 2 3 4 5 6 7 8 9 var a = 10; var c = function(){     this.a = 123;     var d = function(){       console.log(this.a);     };     d(); } var e = new c; 

The result is 10. WINDOW.A is actually the global one. This refers to a function that invokes its context within a function. var d is a separate function, his THIS.A points to the global, no scope, is the global.

?
1 2 3 4 5 6 7 8 9 10 11 12 var a = 10; var b = function(){   console.log(this.a); }var c = function(){   this.a = 123;   this.d = b;   this.d(); }var e = new c;

The result is 123, if THIS.D (), written d (), will error The D is not defined. THIS.D the back of D into the C function. Point execution method, the scope is before the point.

?
1 2 3 4 5 6 7 8 9 var a = 10; var c = function(){     this.a = 123;     this.d = function(){         console.log(a);     };     this.d(); } var e = new c;

The result is 10.

Reference: http://www.cnblogs.com/bennman/archive/2013/09/08/3309024.html

In JavaScript, the context object is the this pointer, the environment in which the called function is located. The purpose of a context object is to reference the object itself that invokes it within a function.


In JavaScript, essentially, a variable of a function type is a reference to the function entity, and assigning a value between references does not produce copy behavior for the object. We can call this function from any reference to a function, except that it is only the context.
Looking closely at the example above, when invoking the same function with a different reference, the this pointer is always the object to which the reference belongs.

The nested relationship of a function scope is determined at the time of definition, not when called, that is, the scope of the JavaScript is static or lexical, because the nested relationships of the scopes can be determined at parse time without waiting for the runtime to determine

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.