JavaScript 中“在默认参数后无默认值的参数”是什么意思
默认参数的出现简化了函数参数处理过程。默认参数允许使用默认值初始化形式参数。只有当未传递任何值或值为 undefined 时这才能实现。有了 ES6,你可以轻松设置默认参数。我们来看一个示例
示例
<html>
<body>
<script>
// default is set to 1
function inc(val1, inc = 1) {
return val1 + inc;
}
document.write(inc(10,10));
document.write("<br>");
document.write(inc(10));
</script>
</body>
</html>以下代码可以正确运行,它展示了从左到右遍历参数的工作原理。即使在后期添加无默认值的参数,它也会覆盖默认参数。
示例
<html>
<body>
<script>
function display(val1 = 10, val2) {
return [val1, val2];
}
document.write(display());
document.write(display(20));
</script>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP