当前位置:网站首页>Function optimization and arrow function of ES6
Function optimization and arrow function of ES6
2022-07-06 20:17:00 【faramita_ of_ mine】
ES6 Function optimization and arrow function of
One 、 Function optimization
① Function parameter defaults
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// Function parameter defaults
// stay ES6 Before , We can't set the default value for a function parameter , You can only use alternative writing :
function add(a, b) {
// Judge b Is it empty , If it is empty, the default value will be given 1
b = b || 1;
return a + b;
}
// Pass a parameter
console.log(add(10));
// Now it can be written like this : Write the default value directly to the parameter , If not, the default value will be used automatically
function add2(a, b = 1) {
return a + b;
}
// Pass a parameter
console.log(add2(20));
</script>
</body>
</html>
② Uncertain parameters
Indeterminate parameters are used to represent the number of indeterminate parameters , Form like ,… Variable name , from … Plus a parameter identifier . Named parameters can only be placed at the end of the parameter list , And there is only one indefinite parameter .
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// Uncertain parameters
function fun(...values){
console.log(values.length)
}
fun(1,2)//2
fun(1,2,3,4)//4
</script>
</body>
</html>
Two 、 Arrow function
ES6 Shorthand for defining functions in :
① When a parameter :
// Previously declared a method
// var print = function (obj){
// console.log(obj);
// }
var print = obj => console.log(obj);
print("hello");
② When there are multiple parameters :
// When there are multiple parameters
//var sum = function (a, b) {
// return a + b;
//}
var sum2 = (a, b) => a + b;
console.log(sum2(11,12));
// When there are multiple parameters
var sum = function (a, b) {
c = a + b;
return a + c;
}
var sum3 = (a, b) => {
c = a + b;
return a + c;
}
console.log(sum3(10,20));
3、 ... and 、 Arrow function combined with deconstruction expression
demand : Declare an object ,hello Method requires individual properties of the object
// The old way :
const person ={
name:"jack",
age:21,
language:['java','js','css']
}
function hello(person){
console.log("hello,"+person.name)
}
hello(person);//hello,jack
const person ={
name:"jack",
age:21,
language:['java','js','css']
}
var hello2 = (param)=>console.log("hello,"+param.name);
hello2(person);//hello,jack
const person ={
name:"jack",
age:21,
language:['java','js','css']
}
// Arrow function + deconstruction
var hello3 = ({
name}) =>console.log("hello,"+name);
hello3(person);//hello,jack
边栏推荐
- [Yann Lecun likes the red stone neural network made by minecraft]
- Period compression filter
- Wechat applet common collection
- AsyncHandler
- Deep learning classification network -- zfnet
- Special topic of rotor position estimation of permanent magnet synchronous motor -- fundamental wave model and rotor position angle
- Standardized QCI characteristics
- 永磁同步电机转子位置估算专题 —— 基波模型类位置估算概要
- 【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN
- Tencent T3 Daniel will teach you hand-in-hand, the internal information of the factory
猜你喜欢
Tencent architects first, 2022 Android interview written examination summary

Discussion on beegfs high availability mode

New generation garbage collector ZGC
腾讯T4架构师,android面试基础

HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
Tencent T2 Daniel explained in person and doubled his job hopping salary

PowerPivot——DAX(初识)
![[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN](/img/b8/3d48e185bb6eafcdd49889f0a90657.png)
[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN

Le lancement du jupyter ne répond pas après l'installation d'Anaconda

系统与应用监控的思路和方法
随机推荐
What happened to the kernel after malloc() was transferred? Attached malloc () and free () implementation source
Wonderful coding [hexadecimal conversion]
Period compression filter
PowerPivot——DAX(初识)
BeagleBoneBlack 上手记
Cesium 两点之间的直线距离
RT thread I2C tutorial
SQL injection 2
Web security - payload
Database specific interpretation of paradigm
Error analysis ~csdn rebound shell error
Groovy basic syntax collation
Unity making plug-ins
Poj1149 pigs [maximum flow]
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发
Deep learning classification network -- zfnet
Web开发小妙招:巧用ThreadLocal规避层层传值
Extraction rules and test objectives of performance test points
recyclerview gridlayout 平分中间空白区域