当前位置:网站首页>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
边栏推荐
- 范式的数据库具体解释
- Le lancement du jupyter ne répond pas après l'installation d'Anaconda
- 小孩子学什么编程?
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- 案例 ①|主机安全建设:3个层级,11大能力的最佳实践
- Discussion on beegfs high availability mode
- Web开发小妙招:巧用ThreadLocal规避层层传值
- 新一代垃圾回收器—ZGC
- 小微企业难做账?智能代账小工具快用起来
- Extraction rules and test objectives of performance test points
猜你喜欢
Digital triangle model acwing 1018 Minimum toll
Jupyter launch didn't respond after Anaconda was installed & the web page was opened and ran without execution
【GET-4】
Notes on beagleboneblack
[Yann Lecun likes the red stone neural network made by minecraft]
Standardized QCI characteristics
Web security - payload
Error analysis ~csdn rebound shell error
5. Nano - Net in wireless body: Top 10 "is it possible?" Questions
设计你的安全架构OKR
随机推荐
设计你的安全架构OKR
Color is converted to tristimulus value (r/g/b) (dry stock)
Example of shutter text component
[network planning] Chapter 3 data link layer (3) channel division medium access control
Appx code signing Guide
BeagleBoneBlack 上手记
B-jiege's tree (pressed tree DP)
永磁同步电机转子位置估算专题 —— 基波模型类位置估算概要
AsyncHandler
报错分析~csdn反弹shell报错
Tencent cloud database public cloud market ranks top 2!
Unity makes AB package
5. Wireless in vivo nano network: top ten "feasible?" problem
POJ 3207 Ikki&#39; s Story IV – Panda&#39; s Trick (2-SAT)
Tencent T3 Daniel will teach you hand-in-hand, the internal information of the factory
PowerPivot——DAX(初识)
微信小程序常用集合
02 基础入门-数据包拓展
Web开发小妙招:巧用ThreadLocal规避层层传值
Leetcode question 448 Find all missing numbers in the array