Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the extension of ECMAscrip function

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use the extension of ECMAscrip function". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use the extension of the ECMAscrip function.

1. Default value of function parameter

1.1 function parameters specify default values

In ECMAScript 2015, default values are allowed to be added to the parameters of a function, and the default values are written directly after the parameters.

The sample code is as follows:

/ / the function parameter specifies the default value function fun (a = 1, b = 2) {console.log (a + b);} fun () / / 3

It is worth noting that parameter variables are declared by default, so you cannot declare them again with let or const, or an exception will be thrown.

In addition, the position of the default parameter is at the end of the parameter list, otherwise ambiguity will occur, and the omitted parameter cannot be omitted.

The following code example:

/ / the position of the default parameter should be at the end of function fun (a = 1, b) {console.log (a, b);} / / call the function, passing a parameter fun (20); / / 20 undefined1.2 is used in conjunction with decoupling assignment

The parameter default value can be used in combination with the default value of the deconstructed assignment. You can set default values for it in two ways. The sample code is as follows:

Function fun ([a, b = 5] = [3]) {console.log (a, b);} fun () / / scope of function parameters

Once the default value of the parameter is set, the parameter forms a separate scope when the function is declared to initialize. When initialization is complete, the scope disappears. This grammatical behavior does not occur when the parameter default value is not set.

The sample code is as follows:

Let x = 5function fun (y = x) {x = 10 console.log (y);} fun () / / 52.rest parameter

ECMAScript 2015 introduces the rest parameter in the form of... Variable name), which is used to get the extra parameters of the function, so that you do not need to use the arguments object. The variable with the rest parameter is an array that puts the extra parameters into the array.

The sample code is as follows:

/ / calculate the maximum value function max1 () {return Math.max.apply (null, arguments)} console.log (max1 (1,5,6,33, 65,35,15)) using the most incoming parameters of the arguments object; / / 65 arg / use the rest parameter function max2 (. Arg) {return Math.max (... arg)} console.log (max2 (1,5,6,33,65,35,15)); / / 653,65,35,15). Arrowhead function

3.1 what is the Arrow function

ECMAScript 2015 adds an arrow function (also known as a fat haircut function), which has a shorter syntax than a function expression and binds this lexically. Arrow functions are anonymous in most cases.

The syntax structure of the arrow function is as follows:

/ / basic syntax structure (parameter 1, parameter 2,..., parameter N) = > {function declaration} (parameter 1, parameter 2,..., parameter N) = > expression (single) / / equivalent to: (parameter 1, parameter 2,..., parameter N) = > {return expression} / / when there is only one parameter The small sign is optional (parameter) = > {function declaration} / / or parameter = > {function declaration} / / No parameter should write a pair of parentheses () = > {function declaration} / / can be used with rest parameter and default parameter (parameter 1, parameter 2,... rest) = > {function declaration} (parameter 1, parameter 2,... parameter N = default N) = > {function declaration}

The arrow function can also define a function name for it, and the syntax structure is as follows:

Let funName = (parameter 1, parameter 2,..., parameter N) = > {function declaration}

The sample code is as follows:

Let sum = (a, b) = > {return a + b} console.log (sum (10,20)); / / considerations for arrow functions

There are several points to note when using the arrow function:

The this object in the body of the function is the object in which it is defined, not the object in which it is used.

The sample code is as follows:

/ / ES5 let fun1 = function () {console.log (this.id);} / / ES6 arrow function let fun2 = () = > {console.log (this.id);} let obj1 = {id: 666, fun: fun1} let obj2 = {id: 666, fun: fun2} obj1.fun () / / 666obj2.fun () / / undefined

From the code, we can see that the this of the ordinary function is determined when it is called, while the this of our arrow function is determined when it is defined.

Cannot be treated as a constructor, that is, you cannot use the new command, or an error will be thrown.

You cannot use an arguments object, which does not exist in the body of the function. If you want to, you can use the rest parameter instead.

4. Tail call of function

Tail call is an important concept in functional programming, which is very simple and can be made clear in one sentence, that is, the last step of a function is to call another function.

The sample code is as follows:

Let x = (x) = > {return 2 * x} let y = function (y) {return x (y)} console.log (y (20)); / / 40 so far, I believe you have a deeper understanding of "how to use the ECMAscrip function extension". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report