function greeting(name =“Anonymous”){默认参数在未指定参数时启动(未定义)。正如您在上面的示例中所看到的,当您未为参数提供值时,参数
返回“你好”+名字;
}
的console.log(问候语( “约翰”)); // 你好约翰
的console.log(问候()); //你好匿名
name
将接收其默认值"Anonymous"
。您可以根据需要为任意数量的参数添加默认值。 increment
加入默认参数,这样它会加1 number
,如果value
未指定。 increment(5, 2)
应为7
。'
testString: 'assert(increment(5, 2) === 7, "The result of increment(5, 2)
should be 7
.");'
- text: increment(5)
的结果应为6
。
testString: 'assert(increment(5) === 6, "The result of increment(5)
should be 6
.");'
- text: 默认参数1
用于value
。
testString: 'getUserInput => assert(getUserInput("index").match(/value\s*=\s*1/g), "default parameter 1
was used for value
.");'
```