当前位置:网站首页>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
边栏推荐
- 方法关键字Deprecated,ExternalProcName,Final,ForceGenerate
- B-杰哥的树(状压树形dp)
- BUUCTF---Reverse---easyre
- 永磁同步电机转子位置估算专题 —— 基波模型类位置估算概要
- HDU 1026 search pruning problem within the labyrinth of Ignatius and the prince I
- HDU 1026 Ignatius and the Princess I 迷宫范围内的搜索剪枝问题
- Qinglong panel white screen one key repair
- How to handle the timeout of golang
- Le lancement du jupyter ne répond pas après l'installation d'Anaconda
- 持续测试(CT)实战经验分享
猜你喜欢
永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
Deep learning classification network -- zfnet
Monthly report of speech synthesis (TTS) and speech recognition (ASR) papers in June 2022
枚举根据参数获取值
【GET-4】
Leetcode question 283 Move zero
[cloud native and 5g] micro services support 5g core network
Oceanbase Community Edition OBD mode deployment mode stand-alone installation
Tencent T2 Daniel explained in person and doubled his job hopping salary
An East SMS login resurrection installation and deployment tutorial
随机推荐
腾讯安卓开发面试,android开发的基础知识
小微企业难做账?智能代账小工具快用起来
Guangzhou's first data security summit will open in Baiyun District
枚举根据参数获取值
案例 ①|主机安全建设:3个层级,11大能力的最佳实践
Jupyter launch didn't respond after Anaconda was installed & the web page was opened and ran without execution
PHP与EXCEL PHPExcel
Tencent byte Alibaba Xiaomi jd.com offer got a soft hand, and the teacher said it was great
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
BUUCTF---Reverse---easyre
腾讯T4架构师,android面试基础
JS implementation force deduction 71 question simplified path
Node. Js: express + MySQL realizes registration, login and identity authentication
腾讯架构师首发,2022Android面试笔试总结
【Yann LeCun点赞B站UP主使用Minecraft制作的红石神经网络】
句号压缩过滤器
报错分析~csdn反弹shell报错
Deep learning classification network -- zfnet
golang的超时处理使用技巧
BeagleBoneBlack 上手记