当前位置:网站首页>Definition and interpretation of global scope and function scope

Definition and interpretation of global scope and function scope

2020-11-10 12:02:52 I'm sorry.

window Global object

Global scope :
Written directly in script In the tag js Code , All in the global scope
Variables in the global scope are global variables , You can access... In any part of the page
Create a global scope when the page opens , Global scope destruction on page close


window Introduction to the object :
window Is an object in the global scope , It represents a browser window , It's created by the browser , We can use it directly .
In global scope , The variables we create will be used as window Object's properties are saved
 Insert picture description here
So we can call variables in the global scope like this



meanwhile , All functions created in the global scope will act as window To save
 Insert picture description here
Objects in the global scope can also be used as window Properties of
 Insert picture description here


Variable declaration ahead of time

Variables are declared ahead of time , What does that mean ?

That is to say , When we use var Declare a variable , So no matter what your statement is in js It's written everywhere in the code that he'll be in Function All code is declared before execution .
such as :
 Insert picture description here

here , although a The declaration of is written in the call a In front of , But there was no error , Just returned one undefined, Below b I didn't write var But there was an error .

reason :
Variable var Whether the statement is written in js Where it will be declared before the execution of all code , But it's just a statement , No assignment in front of

stay js In fact, when we declare variables, we can not use var keyword , Such as this a=123 This is equal to ,window.a=123, however , If we declare that variables do not use var Key words , Then he doesn't declare it before all the code is executed

Function declaration ahead of time

There are two ways to define functions , as follows :
 Insert picture description here
You can see , The first way to define a function is to call , But the second definition method will be wrong before it is called

The first way to define a function :
function hanshu1(){} Will be in Function All code is defined before execution , That is, the statement is advanced

The second way to define a function :
var hanshu2=function(){}; Don't say ahead of time

The first two add Function If you define it in a function like this , Then the defined function or variable cannot be called outside the function .js Nested declaration of functions can be done in

Function definition domain

Create a function scope when calling a function , After the function is executed, the function scope is destroyed .
Every time a function is called, a new function scope is created , They are independent of each other
When you operate on a variable in the scope of a function , It will first look for , If it is not found, it will go back to the upper level to find the global variable
Be careful :
The variables at the upper level are not necessarily global variables , because js Functions in can be nested
such as :
 Insert picture description here
Note that there :
Part of the black coil up , Because of the definition of han() The function is also in hanshu() Internally defined , So it's also an internal function , Can't be in hanshu() Call outside directly , But you can han() Out of function hanshu() Call in function , And then in hanshu() Call out of function hanshu() Function can call han() Function







If you want to access a global scope variable with the same name as the function scope variable in the function scope , So we can think of this variable as window Object to make the call , namely window. Variable name

There is also the early declaration feature in function group scope , as follows :
 Insert picture description here
Function is calling c Later definition c There's no definition of , And then you don't use his outside c

Be careful :
In any variable that is not defined , If it is used directly in a function and the function is called , Then the variables that are not defined in it will become global variables
 Insert picture description here
There's another thing
 Insert picture description here
Writing formal parameters in a defined function is equivalent to defining these parameters in a function
as follows
 Insert picture description here
That's it







版权声明
本文为[I'm sorry.]所创,转载请带上原文链接,感谢