Scope, closure, this point, prototype and prototype chain

1. Scope and free variables

The scope represents the legal scope of a variable, and the scope of a variable is the area of ​​the variable defined in the program source code.

Scope

1. Global scope

Variables that are not declared in any function (the ones that omit var in the function are considered global) are called global variables. Variables
defined in the outermost layer are called global variables, and the global can be used, so it is the global scope.

2. Local scope

Variables declared in a function have function scope and belong to local variables, that is, variables defined inside the function are
only useful inside the function, so they are local scope.

3. Block-level scope defined by ES6

The method of declaring variables is var let const
var. Variables declared can be promoted. Variable promotion is to promote the declaration of the variable to the top of the current scope, and the assignment of the variable will not promote
the variable declared by let/const so that the current code block becomes A temporary dead zone.
Internal variables cannot be used externally. Variables declared by them do not have variable promotion.
Differences: const-declared variables must be default values. const-declared constants cannot be reassigned
. If the value of a const-declared variable is a reference data type, then The data inside the data can be modified

4. Free variables

If a variable a is defined globally and used in a function, this a is a free variable. It can be understood in this way. Any variable that crosses its scope is called a free variable.

2. Closure

A closure is when the parameter of a function is another function, or the return value of a function is another function, it is called a closure.

3. Change the method pointed to by this

Because there are functions nested functions, or other complex structures, it will cause the problem of changing this point. There are three ways to change this point.

There are three methods to change this point, fn.bind(), fn.call(), fn.apply()
they are all methods to change this point. The first parameter is the point of this.
Difference:
bind returns a function
call The parameters are passed to the function in scattered form.
apply is passed in the form of an array.
Bind has a return value. The return value is the function that calls the bind method. The
call does not return a value. His first parameter is the point to this. Whoever is the first parameter, the this inside the function points to whom other parameters are passed to the function in scattered form as actual parameters.
apply has no return value. His first parameter is the point to this. Who is the first parameter, this inside the function points to whom other parameters are passed to the function in the form of an array as actual parameters

4. Prototype and prototype chain

1. The concept of prototype

All JavaScript objects contain a [proto] internal property, which corresponds to
the function object of its own prototype JavaScript. In addition to the prototype [proto], there is also a prototype property.
When the function object is used as a constructor to create an instance When, the prototype property value will be used as the prototype of the instance object [proto]

2. What is the relationship between prototype, constructor, and _ proto_ _

The prototype of the constructor points to the prototype object
The _ proto of the instance object points to the
prototype object of the constructor points The constructor of the prototype object points to the constructor

3. The concept of prototype chain

When an object accesses properties and methods, it will first look for the current object. If it can’t find it, it will go to the prototype of the object. If it finds it, it will use it. If it
can’t find it, it will go to the prototype of the prototype.
The prototype object at the top of the prototype chain , this object is Object.prototype, and
the most commonly used methods such as toString and valueOf are stored in this object. So that we can use these methods in the object

Guess you like

Origin blog.csdn.net/weixin_53687450/article/details/114948886