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 function of ES6 basic syntax

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

The knowledge of this article "ES6 basic Grammar function how to use" is not quite understood by most people, so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "ES6 basic Grammar function how to use" article.

I. extension of function parameters

Default values for ES6 support parameters:

Function fn (name,age,sex= "male") {console.log (`Hello, I am ${name}, gender ${sex}, this year ${age} years old! `);} fn ("Andy Lau", 45); / / Hello everyone, I am Andy Lau, male, 45 years old!

Pass undefined to the default value:

Function fn (name,sex= "male", age) {console.log (`Hello everyone, I am ${name}, gender ${sex}, this year ${age} years old! `);} fn ("Andy Lau", undefined,45); / / Hello everyone, I am Andy Lau, male, 45 years old!

Indefinite parameters: indefinite parameters are used to indicate the number of uncertain parameters:

Function Add (... items) {/ / let sum = 0; / / for (let item of items) / / {/ / sum + = item; / /} / / return sum; let sum = 0; for (var I = 0 var I)

< items.length;i++) { sum += items[i]; } return sum;}let result1 = Add(1,2,3);let result2 = Add(1,3,5,7,9);console.log(result1); //6console.log(result2); //25二、箭头函数 箭头函数提供了一种更加简洁的函数书写方式,基本语法是:参数 =>

Function body

A function with no arguments (the argument section needs to have a pair of empty parentheses):

Let f = () = > {console.log ("hello,world!")} f (); / / hello,world

Equivalent to:

Function f () {console.log ("hello,world!");} f (); / / hello,world

A function with only one argument (the argument part does not need parentheses):

Let f = num = > {if (num%2 = = 0) return "even"; else return "odd";} console.log (f (50)); / / even

A function with multiple arguments (parentheses are required for the arguments):

Let f = (let b) = > {let c = aquib; return c;} console.log (f (2)); / / 5

When there is only one line of statements and you need to return the result, you can omit {}, and the result will be returned automatically:

Let f = (a _ r _ b) = > a _ r _ b _ b _ console.log (f (2p _ 3)); / / 5

When the arrow function is about to return the object, to distinguish it from the code block, wrap the object with ():

Let f = (id,name) = > {id: id,name: name}; let obj = f ("001", "Andy Lau"); / / error console.log (obj); let f = (id,name) = > {return {id: id,name: name};}; let obj = f ("001", "Andy Lau"); console.log (obj) / / {id: "001", name: "Andy Lau"} let f = (id,name) = > ({id: id,name: name}); let obj = f ("001", "Andy Lau"); console.log (obj) / / {id: "001", name: "Andy Lau"} above is the content of this article on "how to use the functions of ES6 basic Grammar". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

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