当前位置:网站首页>ES6 from getting started to mastering 04: default values and remaining parameters of functions
ES6 from getting started to mastering 04: default values and remaining parameters of functions
2022-07-22 05:23:00 【Kai Xiaomo】
explain
ES6 From entry to Mastery Series ( whole 23 speak ) Learning notes .
Functions with parameter defaults
// es5 How to write it
function add(a, b) {
a = a || 666;
b = b || 777;
return a + b;
}
console.log(add());
function add2(a, b = 666) {
return a + b;
}
console.log(add2(2));
The default expression can also be a function
function getVal(val) {
return val + 1;
}
function add3(a, b = getVal(6)) {
return a + b;
}
console.log(add3(3));
The remaining parameters
By three points ...
And a named parameter immediately following it , such as :...keys
, This solves arguments The problem of .
// es5
function getBlogInfo(obj) {
let result = Object.create(null);
for(let i = 1; i < arguments.length; i++) {
result[arguments[i]] = obj[arguments[i]];
}
return result;
}
let blog = {
name: " Kay Xiaomo's blog ",
author: "kaimo",
year: "2017"
}
let blogData = getBlogInfo(blog, "name", "author", "year");
console.log(blogData);
// es6
function getBlogInfo(obj, ...keys) {
console.log(keys);
let result = Object.create(null);
for(let i = 0; i < keys.length; i++) {
result[keys[i]] = obj[keys[i]];
}
return result;
}
let blog = {
name: " Kay Xiaomo's blog ",
author: "kaimo",
year: "2017"
}
let blogData = getBlogInfo(blog, "name", "author", "year");
console.log(blogData);
The remaining parameters contrast arguments:
function checkArgs(...args) {
console.log(args); // Real array
console.log(arguments); // Pseudo array
}
checkArgs("name", "author", "year");
边栏推荐
- Using symbol, ES6 is a new method to obtain key value
- Introduction of (string + memory) function
- Summary of recommendation system papers
- Open source GIS system
- [chestnut sugar GIS] bat - how to delete data with the same suffix under subfolders
- 2022.7.9暑假个人训练1-B.How old are you Mr. String
- to configure. Browserlistrc for browser adaptation
- [chestnut sugar GIS] WPS -- how to fill the space in the content of the previous line
- LeetCode-662-二叉树最大宽度
- Mria + RLOG 新架构下的 EMQX 5.0 如何实现 1 亿 MQTT 连接
猜你喜欢
BGP基础配置,聚合,反射器
Write bootloader from 0 - bootloader relocates app
如何清理C盘
ES6 从入门到精通 # 04:函数之默认值、剩余参数
JVM first acquaintance
全面败退,中国手机首次遭受重大挫折,三星和苹果则大幅增长
Leetcode-662-maximum width of binary tree
Yiyang Qianxi was scolded for being hot search...
mysql存储引擎
Leetcode · daily question · 814 Binary tree pruning recursion
随机推荐
Using symbol, ES6 is a new method to obtain key value
Codeforces Round #807 (Div. 2)
unity的静态设置以及烘培属性
TDengine 在中天钢铁 GPS、 AIS 调度中的落地
PL/SQL 记录
NFS shared storage service
[chestnut sugar GIS] ArcMap - how to quickly generate the four directions information
Mipi based high performance imaging system
Zhenhua scenery semiconductor passed the registration: with an annual revenue of 500million, China Electronics is the actual controller
DRF -- jwt2 user authentication user defined control simpjwt return content
【读书会第13期】+第二章 视频文件的封装格式
Microservice technology development
Butterknife Library (an efficient tool library, Xiaobai only records one usage - no in-depth study, hee hee)
Detailed explanation of file upload basis
Leetcode-153- find the minimum value in the rotation sort array
Brats18 - Multimodal MR image brain tumor segmentation challenge continued 5
【板栗糖GIS】记录两个傻到不能再傻的记录——台式电脑无信号或者不亮屏
Unity URP forward rendering process and obtaining screen space UVs and depth
GTSAM入门学习
Arrange the project publicity schedule to get the most publicity sessions