JS - Scope, Closures

Table of contents

1. Commonly used system functions

isNaN()

2. No parameter function

1. The first call method: button call

2. The second calling method: call fn() function name directly

Functions can execute themselves, not always using click events

3. Pass parameters

1. Pass in the button

 2. Pass parameters directly in the function name: fn(a, b)

 Four, return

1. The function returns the value to the place where it is called through return

2. The function will stop executing and return the specified value 

 5. Variables

1. Global variables

Defined outside the function is a global variable, all scripts and functions on the page can access it.

2. Local variables 

6. Scope

1. Global scope

(1) The system gives the default global object window 

(2) All variables that are not defined and directly assigned are automatically declared to have global scope 

(3) The outermost function (for example: fn below) and variables defined outside the outermost function have global scope​

2. Local scope

Seven, scope chain 

 Nine, closure

A function is a closure, and the closure is to access the variables inside the function

use

  Disadvantage​


1. Commonly used system functions

Example: parseInt("abc") ===> NaN is not a number

isNaN()

Used to check whether its parameter is a non-number, it will convert the string number to a number

Not a number, return turn
is a number, return false


2. No parameter function

1. The first call method: button call

<body>
    <!-- 第一种调用方式 -->
    <button onclick="fn()"></button>
</body>

2. The second calling method: call fn() function name directly

Functions can execute themselves, not always using click events

<script>
        //第二种调用方式
        //函数可以自己执行,不一定总是使用点击事件
        function fn(){
            alert(1)
        }
        fn()
 </script> 

3. Pass parameters

1. Pass in the button


 2. Pass parameters directly in the function name: fn(a, b)


 Four, return

1. The function returns the value to the place where it is called through return


2. The function will stop executing and return the specified value 


 5. Variables

1. Global variables

Defined outside the function is a global variable, all scripts and functions on the page can access it.

Scope: can be accessed globally. (All properties of the window object have global scope, and all variables that are not defined and directly assigned are automatically declared to have global scope.)

Lifetime: Global variables are deleted after the page is closed.


2. Local variables 

Variables declared inside the function (you must use var or let or const to define it, if you don't use it, it will become a global variable.), you can only access it inside the function. (Local variables with the same name can be used in different functions.)

Scope: can only be accessed inside the function.

Lifetime: Local variables are deleted after the function runs (the garbage collection mechanism is referred to as GC), and the variables are released, so the performance will be improved.


6. Scope

        The scope is the accessible range of variables and functions, that is, the scope controls the visibility and life cycle of variables and functions

1. Global scope

All properties of the window object have global scope

(1) The system gives the default global object window 


(2) All variables that are not defined and directly assigned are automatically declared to have global scope 


(3) The outermost function (for example: fn below) and variables defined outside the outermost function have global scope 


2. Local scope

 Contrary to the global scope, the local scope is generally only accessible within a fixed code fragment


Seven, scope chain 

From the inside to the outside, the principle of proximity. (Start from the current search, if you can’t find it, you will search up layer by layer until you find it, if you keep searching up and you can’t find it, you will report an error.

And it has the principle of proximity. If there are variables in the current scope, then the variables in your own scope are given priority. )


 8. Closure

A function is a closure, and the closure is to access the variables inside the function

Purpose: You can read the variables inside the function, so that the values ​​of these variables are always kept in memory.

Disadvantages: 1. The closure will be outside the parent function, changing the value of the variable inside the parent function.

           2. The closure will cause the variables in the function to be stored in memory, which consumes a lot of memory.

use


  shortcoming

 

Guess you like

Origin blog.csdn.net/weixin_68485297/article/details/124546599