The functions of the new characteristics of ES6 default parameters and arrow functions _javascript Tips

Source: Internet
Author: User

1, the function parameter's default value

ES5 cannot directly specify a default value for a function's arguments, only the following workarounds can be used:

From the above code you can see that there is a problem, when the passed parameter is 0 or FALSE, it will be taken directly to the subsequent value, rather than the incoming parameter value.

So how do we solve it? For the code above, you can determine whether to pass in the NUM parameter without using the default value:

This is still cumbersome, and ES6 sets the default value of the function parameter directly in the parameter definition, without worrying that the incoming argument is 0 or false:

2. Arrow function

The arrow function is defined with the => symbol.

The arrow function is equivalent to an anonymous function, so the function expression is used.

On the left is the parameters of the passed function, and the right is the statement executed in the function.

  

The above is the complete writing, the left parenthesis, the right curly brace, and the following situation can be abbreviated:

(1) When the code block to be executed has only one return statement, the curly brace and return keyword can be omitted:

The arrow function is very concise in the callback function, like this:


Note that the arrow functions do not have their own this, arguments, super, and New.target, which point to the corresponding variable of the outer function respectively.

It's a bit of a hassle to use this in ES5 before, and this is a common problem:


But now that you have the arrow function, you no longer need to use that = this or _this = This code, because this in the arrow function is directly the this in the outer function, and the code is simpler:

The above is a small set to introduce a simple ES6 new characteristics of the function of the default parameters and arrow functions, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.