Javascript-what is the variable object of this js code?

Source: Internet
Author: User
{Code...} x is an attribute of window, not a variable? Is y a variable object? Because I have seen this sentence in section 4.2.2 of Javascript advanced programming: "If the var declaration is not used during variable initialization, this variable will be automatically added to the global... Javascript-what is the variable object of this js code?

alert(x); //"x" is not definedalert(b); //"undefinedx = 10;var y = 20;

Does x become an attribute of window, not a variable? Is y a variable object?

Because I have seen such a passage

In section 4.2.2 of Javascript advanced programming, there is a saying: "If the var declaration is not used during variable initialization, the variable will be automatically added to the global environment ."
First, we should clarify that using the var keyword is the only way to declare variables. If there is no var, for example, a = 5, a will be used as an attribute of the global object, rather than a variable.

The differences are as follows:

alert(x); //"x" is not defined alert(b); //"undefinedx = 10; var y = 20;

The first stage after entering the context:

VO = {   x:10; }

In VO, y is not a variable.

Is he right?

Reply content:
alert(x); //"x" is not definedalert(b); //"undefinedx = 10;var y = 20;

Does x become an attribute of window, not a variable?
Is y a variable object?

Because I have seen such a passage

In section 4.2.2 of Javascript advanced programming, there is a saying: "If the var declaration is not used during variable initialization, the variable will be automatically added to the global environment ."
First, we should clarify that using the var keyword is the only way to declare variables. If there is no var, for example, a = 5, a will be used as an attribute of the global object, rather than a variable.

The differences are as follows:

alert(x); //"x" is not defined alert(b); //"undefinedx = 10; var y = 20;

The first stage after entering the context:

VO = {   x:10; }

In VO, y is not a variable.

Is he right?


Simply output the following output... At this time, the window is the context execution environment, right?


Certainly not. variable object is the scope of data related to the execution context ).
It is a special object associated with the context, used to store variables and function declarations defined in the context ). Variables you declare and do not declare are all variables.
Http://www.nowamagic.net/libr...

In fact, there is no need to worry about variables and attributes.

Alert (x) reports an error because x does not use var declaration and is not prefixed to the variable object, when x = 10 is executed, an x attribute is added to the global object.


In the browser, all attributes defined in the global scope are window attributes. Here, x and y are both variables and window attributes. both window. x and window. y can access


Meaning:
Variable Declaration must be completed through the var statement. The global variables that are not declared through the var statement are the properties of the window object.

Differences:
The delete operator is used to delete object attributes.
Variables declared through var and functions declared through function have the DontDelete feature and cannot be deleted.
Global variables that are not declared through var (attributes of the global object) can be deleted.

The above is javascript. what is the variable object of this js code? For more information, see The PHP Chinese website (www.php1.cn )!

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.