Home > front end >  New syntax ES6
New syntax ES6

Time:09-22

Let define variables
Features:
Let's definition of variables, and not to parse
Let's definition of variables, and the forEach () is similar to variables in
Each will define a new, each other does not affect the variable
Especially in the loop, for, forEach, for... Defining variables in, it is best to use the let
Let't repeat defined variable names, a variable name can only be defined once
Let the variables defined, if it is defined in {}, can only be executed in {} is called
Outside the {}, is cannot be invoked to perform {} - if the switch for the forEach for... While in the function
Let in different {}, is a different scope definition let variable
At this point, the name of the variable and the variable's name in the other {}, {} or outside of the variable name, can be repeated

Const define variables
In JavaScript, often tube const variables defined, called constants const variables defined, cannot be repeated assignment, data have been defined, cannot be changed,
Const defined variable names, can't repeat const defined in {}, cannot be in {} calls
Generally use const defined object, array, function, and the reference data type
Const reference data is stored in the type of address, as long as the address don't change, can change the array, the object unit stored data

Arrow function
So-called arrow function is the function of another kind of grammar form
Const fun=function () {}
normal functionConst fun=()=& gt; {} function arrow
Parts of the anonymous functions, from the function () {} ()=written & gt; The form of {}

If the function is only one parameter, can not write a ()
Const fun=function (e) {}
normal functionConst fun=e=& gt; {} function arrow

If only one line of code in the body, can not write a {}
Const fun=e=& gt; {the console. The log (e)}
ordinary arrows functionConst fun=e=& gt; The console. The log (e) not write a {} arrows function
About this arrow function point to
In the arrow function, this point, this points to the parent program
If the parent program has this point, then the arrow function point is the parent program of this
If the parent program without this point, then point to is the window
Summary about this
1, ordinary function function
Declarative window
-- -Assign a value type window
-- -The forEach loop window
-- -Timer, delay timer window
-- -The function in the object -- -- --
object itselfBinding event handling function - binding events tag

2, this arrow function point to
This point to the parent program
If the parent program has this point (the parent program is also a function), this point is the parent program of this point to
If the parent program does not have this point (array, object... ), and this points to the window
key : arrow function, can't change this point, only the ordinary function function, can change this point to

Alter this point to
1, call () method
Grammar: function call (parameter 1, other parameters... Can be multiple or no)
Call and executive function, at the same time, the function of this point, defined as the contents of the specified parameters (1)
Parameter 1, is to change this point to
Other parameters, it is the original function arguments, the original function has several parameters, at this point to the corresponding input several arguments, no parameters, no argument

2, the apply () method
Grammar: function. The apply parameters (parameter 1, 2)
there are only two parametersParameter 1: change the points to the content of this
Parameter 2: original function arguments, must be in the form of an array, will define the arguments as an array element
Other USES and effects on the same call

Conclusion: call method and the apply methods, function, effect, is completely consistent
Just for the parameters of the original function assignment method, different
Call method is implemented by several other parameters
The apply method is through an array parameter, to implement the
There is no essential difference of two methods

3, the bind () method
Grammar: const variable=function. The bind parameters (1);
Not executed immediately function
Generate a new function, this new function is change after this point to the new function of
Parameter 1, defined to change this point to
Other parameters, generally does not define, is to use the function of the original parameter

CodePudding user response:

CodePudding user response:

Conclusion good
  • Related