Home  >  Article  >  Backend Development  >  javascript - What is the variable object of this js code?

javascript - What is the variable object of this js code?

PHP中文网
PHP中文网Original
2016-08-25 10:37:221257browse

javascript - What is the variable object of this js code?

alert(x); //"x" is not definedalert(b); //"undefined

x = 10;
var y = 20;

Has x become an attribute of window instead of a variable? Is y a variable object?

Because I saw this paragraph

There is this sentence in Section 4.2.2 of "Javascript Advanced Programming": "If a variable is initialized without a var declaration, the variable will automatically be added to the global environment."
First of all, we should be clear about something , using the var keyword is the only way to declare a variable. If there is no var, for example a = 5, a will be used as a property of the global object instead of a variable.

The difference is as follows:

alert(x); //"x" is not defined alert(b); //"undefined

x = 10; var y = 20;

The first stage after entering the context:

VO = {   x:10; }

The reason why there is no y in VO is that y is not a variable.

Is he right?

Reply content:

alert(x); //"x" is not defined
alert(b); //"undefined

x = 10;
var y = 20;

x has become a property of window instead of a variable?
y is a variable object?

Because I saw such a paragraph

There is this sentence in section 4.2.2 of "Javascript Advanced Programming": "If a variable is initialized without a var declaration, the variable will automatically be added to the global environment."
First of all, 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 used as a property of the global object instead of a variable.

The difference is as follows:

alert(x); //"x" is not defined alert(b); //"undefined

x = 10; var y = 20;

The first stage after entering the context:

VO = {   x:10; }

The reason why there is no y in VO is that y is not a variable.

Excuse me, is he right?


Just type it below and see if it’s correct. . . The window at this time is the context execution environment, right?


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

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

alert(x)会报错就是因为x没有使用var声明,不会被前置到变量对象中,当执行x=10的时候才会去给全局对象上添加一个x属性。


In the browser, all properties defined in the global scope are window properties. Here x and y are both variables and properties of window. Both window. The global variables are properties of the window object.

Difference: The
delete operator is used to delete object attributes.

Variables declared through var and functions declared through function have the DontDelete attribute and cannot be deleted.

Global variables (attributes of global objects) that are not declared with var can be deleted

The above is the content of javascript - What is the variable object of this js code? For more related content, please pay attention to the PHP Chinese website (www.php .cn)!




Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:app recommendation expertNext article:app recommendation expert