JavaScript-What is the variable object for this JS code?

Source: Internet
Author: User
JavaScript-What is the variable object for this JS code?

alert (x); "X" is not Definedalert (b); "Undefinedx = 10;var y = 20;

Does x become a property of window rather than a variable? y is a variable object?

' Cause I saw this conversation.

In the JavaScript advanced Programming 4.2.21, there is a sentence: "If the variable is initialized without using the Var declaration, the variable is automatically added to the global environment." ”
First, we should make it clear that using the var keyword is the only way to declare a variable. If there is no Var, for example A = 5, a will be a property of the global object, not a variable.

The difference is as follows:

alert (x); "X" is not defined alert (b); "Undefinedx = 10; var y = 20;

Enter the context after the first stage:

VO = {   x:10;}

The reason why VO does not have y is that Y is not a variable.

Is he right, please?

Reply content:

alert (x); "X" is not Definedalert (b); "Undefinedx = 10;var y = 20;

Does x become a property of window rather than a variable?
Y is a variable object?

' Cause I saw this conversation.

In the JavaScript advanced Programming 4.2.21, there is a sentence: "If the variable is initialized without using the Var declaration, the variable is automatically added to the global environment." ”
First, we should make it clear that using the var keyword is the only way to declare a variable. If there is no Var, for example A = 5, a will be a property of the global object, not a variable.

The difference is as follows:

alert (x); "X" is not defined alert (b); "Undefinedx = 10; var y = 20;

Enter the context after the first stage:

VO = {   x:10;}

The reason why VO does not have y is that Y is not a variable.

Is he right, please?


Output directly below it. The window at this time is the context execution environment, right?


Definitely not. The Variable object (variable object) is the data scope (scope of) that is related to the execution context.
It is a special object associated with the context that stores variables (variables) and function declarations (functions declarations) that are defined in the context. Variables that you declare, do not declare, are variables.
Http://www.nowamagic.net/libr ...

There is no need to dwell on variables and attributes at all.

Alert (x) causes an error because X does not use the Var declaration and is not pre-placed into the variable object, and when the x=10 is executed, an X attribute is added to the global object.


In the browser, the properties of the window are defined in the global scope. Here x and y are both variables and properties of the window, and both window.x and WINDOW.Y can access the


Meaning:
A variable declaration must be done through a VAR statement, and a global variable declared without a var statement is a property of the Window object.

Difference:
The delete operator is used to delete object properties.
Variables declared through Var and functions declared by function have the Dontdelete attribute and cannot be deleted.
Global variables declared without VAR (properties of global objects) can be deleted

The above is JavaScript-what is the variable object of this JS code? Please pay attention to topic.alibabacloud.com (www.php.cn) for more relevant content.

  • 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.