什么是"JavaScript 中默认参数后没有默认值的参数"
javascriptweb developmentfront end technology
默认参数可以轻松处理函数参数。默认参数允许您使用默认值初始化形式参数。这仅在未传递任何值或未定义时才有可能。使用 ES6,您可以轻松设置默认参数。让我们看一个例子
示例
<html>
<body>
<script>
// 默认值设置为 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>
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

