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;Copy after loginCopy after loginThe first stage after entering the context:
VO = { x:10; }Copy after loginCopy after loginThe reason why there is no y in VO is that y is not a variable.
Is he right?
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;Copy after loginCopy after loginThe first stage after entering the context:
VO = { x:10; }Copy after loginCopy after loginThe 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.
Global variables (attributes of global objects) that are not declared with var can be deleted