Home  >  Article  >  Web Front-end  >  Javascript Study Notes 3 Scope_Basic Knowledge

Javascript Study Notes 3 Scope_Basic Knowledge

WBOY
WBOYOriginal
2016-05-16 18:36:51867browse

In Javascript, the global environment itself is an object. In the browser host, this object is window, but when Javascript is used in other non-browser hosts, such as embedded environments, it may be other objects.
I would also like to correct a concept here. Many people think that Javascript is only used in browsers. In fact, Javascript can also be used in many non-Web situations. According to reports, Javascript also performs very well in some embedded-based application fields. Excellent, of course I have only heard these legends.
Getting back to the subject, when we write: var i=1, we are actually declaring a variable in the window scope.
When we write i=1, we declare a window attribute.
Look at this piece of code:


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

This code is output separately The result is: undefined world, welcome china, hello china.
Let’s explain them separately:
In the above, we said that when Javascript is precompiled, all var variables will be created, and the default value is undefined. We Here is an example:
We can write a piece of code like this:

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
When we execute this section When scripting, you can find that undefined pops up first, and then a script error is prompted, indicating that b does not exist. It can be shown that a has been created and initialized to undefined during the precompilation process, but b can only be interpreted sequentially during actual runtime. In fact, the precompiled Javascript code can be almost understood as follows:
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute it
]

We can talk about it next The problem of function scope. Whenever the code runs into a function, the Javascript engine will automatically create a new scope, then use this new scope as a child scope of the current scope, and then switch the current code scope. to this new scope. When the code exits the function, this scope is destroyed, returning the code scope to its parent scope.
Okay, the preparations are almost done. Next we will explain the first question: asking why undefined world will be output. First, the code is precompiled. When entering the Test method, a new scope is opened, and then the global scope is used as its parent scope. Then precompile the method in Test. Like the above code, the method body after the Test method is precompiled is roughly as follows: Copy code

The code is as follows:


function Test() {
var a = undefined;
alert(a " " b);
var a = "welcome";
b = "china";
alert(a " " b);
}


Of course, b cannot be found in the current scope, so he will go to his b="world" is found under the parent scope, that is, the global scope. So this result was produced.
Welcome china pops up for the second time, there is nothing to say.
The third time, hello china pops up. We can understand that var a is just a local variable of method Test, and since b is not declared in advance, it will go to the parent scope to find the corresponding definition.
[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute ]
首先,我们将方法体内的var a改成a,我们先不看答案,直接来分析,首先,在预编译阶段,方法体内几乎没有任何改变,因此此时a和b一样,都要去他们的父作用域中去寻找,因此第一次出的结果应该是hello world,第二次没什么说的:welcome china,第三次由于a和b在本作用域内都没有事先定义,因此都是再改变父作用域内的值,因此应该输出welcome china.
我们继续:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

和上面的分析一样,应该输出undefined undefined,welcome china,hello world.
继续:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

应该是undefined world,welcome china,hello china.
经试验,都没问题, 不知道你明白了么?
因此我们可以得出,每个变量在找不到自己的定义时,都会沿着作用链向上寻找,这样就很可能会出现未预知的错误,给排错添加了很多困难。更麻烦的是,还可能会对父作用域上的变量值进行修改,因此我们在声明变量时应该尽量加上var,尽管Javascript并不强迫我们这样做。
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