What is the scope of the object in Javascript-js? Or is there some question about the properties of the object?

Source: Internet
Author: User
var a=3;var object = {     a:2,     run: function() {         alert(a);         //a=3;         }     

A why not 2?
What does the a:2 in the object mean?

Reply content:

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

A why not 2?
What does the a:2 in the object mean?

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

You do not display a variable in the specified or global scope.

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

Scope of the problem, you directly pop up a, this time the program will find a, first in the current scope to find, no, will go outside to find, and then found the Var A, this a is actually equivalent to WINDOW.A, in fact, you here directly pop up a A, it is equivalent to find a WINDOW.A the global scope of a, and why is not object.a this a, said before, you look for the equivalent of WINDOW.A rather than obejct.a the scope of the two environments. JS as an object-oriented programming language, all things have a presence, which belongs to which, this to understand, if you do not, it is equivalent to the default it is the global below, the general JS all of the east is defined in the window below. So if you're not going to define which a is, then a is a direct window. Of course, the functions defined below are equivalent to those within the scope of the function. Not much of the concrete said. 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.