To learn js, you must learn the scope. There is still a big difference between the scope of js and other mainstream languages.
1.js has no block-level scope.
js has no block-level scope, like this:
if(){ var a = 100; (a) //Output 100} (a) //Output100
Statements like if, for, switch in js, the variables in the code block they contain can also be read outside the code block, so js has no block-level scope.
Two global variables of .js
JS stipulates that global variables can be regarded as properties of windows, and global variables can be read by all code blocks.
var a = 10; function() { b = 20; (a); //Output 10;} (b); //Output20;
Although a is not defined in an anonymous function, since a is a global variable, any other code block can read the value of a. In a complex project, if global variables are not operated carefully, they are likely to cause major bugs. Therefore, when writing code, you should try to avoid using global variables! For a variable, if it is not declared with var, it will automatically be considered a global variable. Therefore, in writing, var must not be missed.
Local variables of three.js
Global variables in js can easily cause problems in the code, so we should clearly distinguish between global variables and local variables! Local variables are only read inside the function where they are located, but cannot be read outside the function.
function doSomething(){ var blogName="Zhixuan Capital"; function innerSay(){ alert(blogName); } innerSay(); } alert(blogName); //undefined innerSay(); //undefined
Four.js scope chain problem
Since js has global variables and local variables, when calling a variable, it will search for its scope chain. If this variable is defined inside the function, then take the value of the variable. If not, then search in the upper layer. If it is found, get this value. If it is not found, continue to search in the upper layer until the position is found. If it is found and not found in the end, then the value of the variable is undefined.
Let’s take a look at an example:
var myName = 'Zhixuan Capital'; function scoap() { (myName); var myName = "zhixuan"; (myName); (age); } scoap();
Let’s analyze this example first. Scoap() will call this function. The first one will search for the name value. First, see if the function Scoap is defined inside. I found that the name is defined inside the function, so the first one will no longer be searched upward! So is the first value of (name) "zhixuan"? no! no! no! Since the name has not been assigned a value at the first (name), the first (name) is undefined and the second (name) is "zhixuan"!
Let’s take a look at another example:
var a = 10; function zhixuan() { (a); } function ziben() { var a = 20; zhixuan(); } ziben();
What is the value of (a) this time? First, execute the ziben() function, which defines a to 20, and then execute the zhixuan() function, requiring the output of a value. Since the scope is determined at the moment of the function definition, the zhixuan() function will find the global variable of a upward, that is, var a=10, instead of performing the scope search in ziben()! So (a) is 10.
Of course, my understanding of these is relatively shallow. If you want to continue in-depth, I recommend reading:https:///article/
The above is all the content of this article. I hope the content of this article will be of some help to everyone’s study or work. If you have any questions, you can leave a message to communicate. At the same time, I hope to support me more!