xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js ...rest & ...spread All In One

js ...rest & ...spread All In One

bug

function func(a, rest, c) {
    console.log('a, rest, c =', a, rest, c);
}

const arr = ['a', 'b', 'c'];

// ...rest 只能用于最后一个参数
func(1, ...arr, 3);
// a, rest, c = 1 a b

// ❌
function func(a, ...rest, c) {
    console.log('a, rest, c =', a, rest, c);
}

// ✅  ...rest 只能用于最后一个参数
function func(a, b, ...rest) {
    console.log('a, b, rest =', a, b, rest);
}

refs

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-04-21 15:57  xgqfrms  阅读(23)  评论(1编辑  收藏  举报