小编Eri*_*pin的帖子

ES6默认参数:未正确分配的值

我正在玩ES6默认参数,我有一个奇怪的行为.

以下是该问题的一个简短示例:

function test(firstValue, secondValue=5){
  console.log("firstValue: " + firstValue);
  console.log("secondValue: " + secondValue);
  console.log("----------")
}

test(2, secondValue = 3)
test(secondValue = 3)
Run Code Online (Sandbox Code Playgroud)

它的输出:

firstValue: 2
secondValue: 3
----------
firstValue: 3
secondValue: 5
----------
Run Code Online (Sandbox Code Playgroud)

在第二种情况下,我期待firstValue: undefinedsecondValue: 3.这种行为是否正常.我错过了什么吗?

javascript default-parameters ecmascript-6

1
推荐指数
1
解决办法
90
查看次数