本篇文章给大家带来的内容是关于es6中剩余参数的代码讲解,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
剩余参数将没有对应形参的参数聚合成一个数组
function(a, b, ...theArgs) {
}剩余参数只会将没有对应形参的参数聚合成一个数组, 而arguments则是包含了所有的参数。
function add(a, b, ...theArgs) {
return {rest: theArgs, arguments}
}
add()
// {rest: [undefined, undefined, []], arguments: Arguments(0)}
add(1)
// {rest: [1, undefined, []], arguments: Arguments(1)}
add(1, 2)
// {rest: [1, 2, []], arguments: Arguments(2)}
add(1, 2, 3, 4, 5)
// {rest: [1, 2, [3, 4, 5]], arguments: Arguments(5)}剩余参数始终是一个数组,而不像arguments是一个伪数组
本书是全面讲述PHP与MySQL的经典之作,书中不但全面介绍了两种技术的核心特性,还讲解了如何高效地结合这两种技术构建健壮的数据驱动的应用程序。本书涵盖了两种技术新版本中出现的最新特性,书中大量实际的示例和深入的分析均来自于作者在这方面多年的专业经验,可用于解决开发者在实际中所面临的各种挑战。
466
function add(...theArgs) {
console.log(Array.isArray(theArgs))
theArgs.forEach((a)=>console.log(a))
console.log(Array.isArray(arguments))
Array.prototype.slice.call(arguments, add.length).forEach((a)=>console.log(a)) // 转化成数组
}
add(1,2,3) // true 1 2 3 false 1, 2, 3, 4function add(...[a, b, c]){
return a + b +c
}
add(1, 2, 3) // 6
add(1, 2, 3) // 6babel翻译function add(...num){
return num.reduce((n1,n2)=>n1+n2)
}翻译后
function add() {
for (var _len = arguments.length, num = Array(_len), _key = 0; _key < _len; _key++) {
num[_key] = arguments[_key];
}
return num.reduce(function (n1, n2) {
return n1 + n2;
});
}以上就是ES6中剩余参数的示例讲解的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号