当前位置:网站首页>[ecmascript6] function and its related use
[ecmascript6] function and its related use
2022-07-28 13:26:00 【Willard Leozi】
1、 Functions with parameter defaults
// es5
function es5_add(a, b) {
a = a || 1;
b = b || 2;
return a + b;
}
console.log(es5_add());
//3
// es6
function es6_add(a=1, b=2) {
return a + b;
}
console.log(es6_add());
//3
function es6_add1(a, b=2) {
return a + b;
}
console.log(es6_add1(2));
//4As you can see from the code above ,es5 Function parameters in are not allowed to be set with default values , and es6 You can and can mix parameters with and without default values .
2、 The default expression can also be a function
function es6_add(a=1, b=getBVal(5)) {
return a + b;
}
function getBVal(val) {
return val+1;
}
console.log(es6_add(2));
// 83、 Unnamed parameter function
function pick(obj) {
let result = Object.create(null);
for(let i=1; i<arguments.length; i++) {
result[arguments[i]] = obj[arguments[i]];
}
return result;
}
let book = {
title: 'es6',
author: 'Leozi',
year: 2021
}
let bookData = pick(book, 'author', 'year');
console.log(bookData);es6 The remaining parameters : from ... And a named parameter immediately following it (...keys).
Be careful ...keys It must be placed later in the function parameter , Otherwise an error .
function pick(obj, ...keys) {
console.log(keys);
// (2) ['author', 'year']
let result = Object.create(null);
for(let i=0; i<keys.length; i++) {
result[keys[i]] = obj[keys[i]];
}
return result;
}
let book = {
title: 'es6',
author: 'Leozi',
year: 2021
}
let bookData = pick(book, 'author', 'year');
console.log(bookData);
// {author: 'Leozi', year: 2021}
function test(...args) {
console.log(args);
// (3) [1, '2', Array(1)]
console.log(arguments);
// Arguments(3) [1, '2', Array(1), callee: (...), Symbol(Symbol.iterator): ƒ]
}
test(1, '2', [3]);args Get the real array of formal parameters ,arguments The result is a pseudo array of formal parameters .
4、 Extension operator
Remainder operator : Combine multiple independent parameters into an array .
Extension operator : Split an array , And pass each term as a separate parameter to the function .
// Find the maximum value of the determined formal parameter
const maxNum = Math.max(1, 2);
console.log(maxNum);
// 2
// Handle the maximum value in the array ,es5 Use in apply
const arr = [1, 6, 3, 8, 9, 5];
console.log(Math.max.apply(null, arr));
// 9
// es6 Use the extension operator in
console.log(Math.max(...arr));
// 95、es6 Arrow function function () {} Equivalent to ()=>{}
// es5 Middle function
let add = function ( a, b) {
return a + b;
}
console.log(add(1, 2));
// 3
// es6 neographism
let add = (a, b) => {
return a + b;
}
console.log(add(1, 2));
// 3
let add1 = val => {
return val + 5;
}
// If the return value is an expression, you can write it directly , And it can return normally
let add2 = val => val;
let add3 = val => (val + 5);
let add4 = (val1, val2) => (val1 + val2);
let add4 = (val1, val2) => val1 + val2;
let fn = () => 'hello world';
console.log(fn());
// 'hello world'
let fn1 = () => 'hello world ' + 123;
console.log(fn1());
// 'hello world 123'
let getObj = id => {
return {
id: id,
name: 'Leozi'
}
}
console.log(getObj());
// Closure function
// es5:
let fn = (function () {
return function () {
console.log('es5');
}
})();
fn();
// es6:
let fn_es6 = (() => {
return () => {
console.log('es6');
}
})();
fn();6、rest Parameters
function fn() {
console.log(arguments); // arguments Is a pseudo array , Its prototype is the object
}
fn('1', '2', '3');
function fn1(...args) {
console.log(args); // rest Parameters args True array , Its prototype is array
}
fn1('1', '2', '3');
function fn2(a, b, ...args) { // args It can only be placed at the end of the parameter list
console.log(a) // 1
console.log(b) // 2
console.log(args) // ['3', 4, 5, 6]
}
fn2('1', '2', '3', 4, 5, 6)Use rest Benefits of parameters : Because the parameter corresponds to the true array , You can directly use some special API Method , Such as filter、some、every wait .
边栏推荐
- Basic exercises of DQL in MySQL
- Aragon创建DAO polygon BSC测试网
- 二舅能治好年轻人的精神内耗吗?
- Leetcode daily question (2196. create binary tree from descriptions)
- Why neural networks are ineffective?
- Chinese translation of pointnet:deep learning on point sets for 3D classification and segmentation
- PHP生成随机数(昵称随机生成器)
- 【嵌入式C基础】第9篇:C语言指针的基本用法
- vim常用命令详解(vim使用教程)
- Gamestop bear market entered NFT trading, and established game retailers took advantage of Web3 to make a second spring
猜你喜欢

Intrinsic value and time value of options
![[embedded C foundation] Part 5: original code / inverse code / complement code](/img/85/16d5e9236e13870197d12386e3ab15.png)
[embedded C foundation] Part 5: original code / inverse code / complement code

Machine learning Basics - integrated learning-13

Risk analysis of option trading

Auto.js enables Taobao to quickly submit orders
![[FPGA] FIR filter - half band filter](/img/6e/d97b3842f80e37aa41b888384a14cb.png)
[FPGA] FIR filter - half band filter
![[embedded C foundation] Part 2: binary conversion and BCD coding](/img/12/d9a42cf7b4dc177d00e5dc3cdaa5cd.png)
[embedded C foundation] Part 2: binary conversion and BCD coding

Call / put option price curve

Bear market spread portfolio

屈辱、抗争、逆转,三十年,中国该赢微软一次了
随机推荐
JS method of splitting strings
Black cat takes you to learn EMMC Protocol Part 26: hardware reset operation of EMMC (h/w reset)
Dimming and color matching cool light touch chip-dlt8ma12ts-jericho
Leetcode-190. inverting binary bits
沾上趣店,都得道歉?
基于深度学习的超分辨率重建
2020jenkins study notes
[error] after logging in to another machine using SSH, you find that the hostname is still yourself | unable to access yarn8088
jar包
Intrinsic value and time value of options
Aragon creates Dao polygon BSC test network
【C语言易错点】第4篇:结构体在内存中存储规则详讲
Redis - Basics
Dry goods -- encapsulated anti shake and throttling method in the project
Risk analysis of option trading
Complete set of SSM framework online bookstore
Why neural networks are ineffective?
Call / put option price curve
【嵌入式C基础】第3篇:常量和变量
Rust 从入门到精通01-简介