当前位置:网站首页>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
边栏推荐
- AddressSanitizer 技术初体验
- 02 基础入门-数据包拓展
- 棋盘左上角到右下角方案数(2)
- Tencent T2 Daniel explained in person and doubled his job hopping salary
- 02 basic introduction - data package expansion
- Standardized QCI characteristics
- 2022年6月语音合成(TTS)和语音识别(ASR)论文月报
- Tencent byte and other big companies interview real questions summary, Netease architects in-depth explanation of Android Development
- JMeter server resource indicator monitoring (CPU, memory, etc.)
- 22-07-05 upload of qiniu cloud storage pictures and user avatars
猜你喜欢

Oceanbase Community Edition OBD mode deployment mode stand-alone installation

Tencent byte Alibaba Xiaomi jd.com offer got a soft hand, and the teacher said it was great

小孩子学什么编程?

Anaconda安装后Jupyter launch 没反应&网页打开运行没执行
Tencent Android development interview, basic knowledge of Android Development

BUUCTF---Reverse---easyre

Standardized QCI characteristics

【云原生与5G】微服务加持5G核心网

Example of shutter text component

棋盘左上角到右下角方案数(2)
随机推荐
使用ssh连接被拒
Oceanbase Community Edition OBD mode deployment mode stand-alone installation
枚举根据参数获取值
Method keywords deprecated, externalprocname, final, forcegenerate
Maximum likelihood estimation and cross entropy loss
永磁同步电机转子位置估算专题 —— 基波模型类位置估算概要
Tencent byte Alibaba Xiaomi jd.com offer got a soft hand, and the teacher said it was great
Synchronization of data create trigger synchronization table for each site
微信小程序常用集合
Special topic of rotor position estimation of permanent magnet synchronous motor -- Summary of position estimation of fundamental wave model
Enumeration gets values based on parameters
Standardized QCI characteristics
【GET-4】
Leetcode question 283 Move zero
Le lancement du jupyter ne répond pas après l'installation d'Anaconda
数据的同步为每个站点创建触发器同步表
[cloud native and 5g] micro services support 5g core network
recyclerview gridlayout 平分中间空白区域
Digital triangle model acwing 1018 Minimum toll
报错分析~csdn反弹shell报错