What is the scope of objects in javascript-JS? Or are there some questions about object attributes?

Source: Internet
Author: User
{Code...} why isn't a 2? What does a: 2 in an object mean?
var a=3;var object = {     a:2,     run: function() {         alert(a);         //a=3;         }     }; object.run(); 

Why is a not 2?
What does a: 2 in an object mean?

Reply content:
var a=3;var object = {     a:2,     run: function() {         alert(a);         //a=3;         }     }; object.run(); 

Why is a not 2?
What does a: 2 in an object mean?

run: function() {     this.a=1;     } }; 

You do not display the specified or global variables.

var a=3;var object = {     a:2,     run: function() {         alert(this.a);//2         alert(a);//3         }     }; object.run(); 

If the scope problem occurs, you directly pop up a. at this time, the program will look for a. First, it will look for a in the current scope. if it does not, it will look for a outside, then we find var a, which is actually equivalent to window. a, in fact, a is directly displayed here, which is equivalent to finding a window. a. Why is it not an object in the global scope of. a. What about a? as mentioned above, you are looking for a window. a instead of obejct. a has different scopes. Js, as an object-oriented programming language, must have an ownership of all things. if you don't do anything, by default, it is global. generally, all js stuff is defined under the window. So if you decide which a is, a is equivalent to window. Of course, the function defined below is equivalent to the function scope. I will not talk about it more specifically. Read more information.

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.