1. The default value of function parameters
In ES5, you cannot directly specify the default value for the parameters of the function, and you can only use the following workarounds:
From the above code, we can see that there is a problem. When the passed parameter is 0 or false, the subsequent value will be directly taken, rather than the passed parameter value.
So how to solve it? For the code in the figure above, you can determine whether the num parameter has been passed in. If not, use the default value:
This approach is still very troublesome, while ES6 directly sets the default value of function parameters in the parameter definition, and there is no need to worry about passing in the parameter 0 or false that will make an error:
2. Arrow function
The arrow function is defined with the => symbol.
Arrow functions are equivalent to anonymous functions, so function expressions are written.
On the left are the parameters of the incoming function, and on the right are the statements executed in the function.
The above is the complete writing method, with brackets on the left and braces on the right, and the following situations can be abbreviated:
(1) When the code block to be executed has only one return statement, the braces and return keywords can be omitted:
The arrow function is very concise in the callback function, like this:
It should be noted that arrow functions do not have their own this, arguments, super, and new.target, which point to the corresponding variables of the outer function respectively.
It was a bit troublesome to use this in ES5 before, and this kind of problem is very common:
But now that with the arrow function, you no longer need to use code like that = this or _this = this, because this in the arrow function is directly this in the outer function, and the code is simpler:
The above is the default parameters and arrow functions of A and the new features of ES6 introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!