Javascript ES6默认参数:未正确分配值
我在玩ES6默认参数,我有一个奇怪的行为 下面是一个简单的问题示例:Javascript ES6默认参数:未正确分配值,javascript,ecmascript-6,default-parameters,Javascript,Ecmascript 6,Default Parameters,我在玩ES6默认参数,我有一个奇怪的行为 下面是一个简单的问题示例: function test(firstValue, secondValue=5){ console.log("firstValue: " + firstValue); console.log("secondValue: " + secondValue); console.log("----------") } test(2, secondValue = 3) test(secondValue = 3) 其输出:
function test(firstValue, secondValue=5){
console.log("firstValue: " + firstValue);
console.log("secondValue: " + secondValue);
console.log("----------")
}
test(2, secondValue = 3)
test(secondValue = 3)
其输出:
firstValue: 2
secondValue: 3
----------
firstValue: 3
secondValue: 5
----------
在第二种情况下,我期望
firstValue:undefined
和secondValue:3
。这种行为正常吗。我遗漏了什么吗?您正在使用一个全局变量,但没有声明它。赋值生成一个全局变量,不影响函数test
的变量。这些局部变量独立于调用范围
功能测试(第一个值,第二个值=5){
log(“firstValue:+firstValue”);
console.log(“secondValue:+secondValue”);
console.log(“------------”)
}
var secondValue;//带声明
console.log(secondValue);
测试(2,秒值=3)
测试(第二个值=3)
控制台日志(秒值)代码>您正在使用一个全局变量,但没有声明它。赋值生成一个全局变量,不影响函数test
的变量。这些局部变量独立于调用范围
功能测试(第一个值,第二个值=5){
log(“firstValue:+firstValue”);
console.log(“secondValue:+secondValue”);
console.log(“------------”)
}
var secondValue;//带声明
console.log(secondValue);
测试(2,秒值=3)
测试(第二个值=3)
console.log(secondValue)代码>我猜您想进行分解:
函数测试({firstValue,secondValue=5}={}){
log(“firstValue:+firstValue”);
console.log(“secondValue:+secondValue”);
console.log(“------------”)
}
test()//打印第一个值:未定义,第二个值:5
test({})//打印第一个值:未定义,第二个值:5
test({firstValue:2})//打印firstValue:2,secondValue:5
test({secondValue:3})//打印firstValue:undefined,secondValue:3
您想进行分解,我想:
函数测试({firstValue,secondValue=5}={}){
log(“firstValue:+firstValue”);
console.log(“secondValue:+secondValue”);
console.log(“------------”)
}
test()//打印第一个值:未定义,第二个值:5
test({})//打印第一个值:未定义,第二个值:5
test({firstValue:2})//打印firstValue:2,secondValue:5
test({secondValue:3})//执行此操作时打印firstValue:undefined,secondValue:3
test(2, secondValue = 3)
实际上,您正在这样做:
secondValue = 3
test(2, 3)
第一部分(secondValue=3
)创建了一个名为secondValue
的全局变量,这要归功于。*它与函数中的secondValue
参数无关。JavaScript没有命名参数。(例如,在调用时不能说“这是secondValue
”的值,除非将其放在参数列表中的正确位置。如果要在调用时指定名称,可以使用destructuring as,它不是真正命名的参数,但可以达到相同的目的。)
第二部分(将3
传递到test
)发生的原因是赋值的结果是赋值的值(因此secondValue=3
将secondValue
设置为3
,并生成值3
,然后传递到test
)
要使用3
调用test
以获得secondValue
,只需这样做。例如:
test(2, 3);
如果要禁用第二个选项,将使用默认选项:
test(2);
例如:
功能测试(第一个值,第二个值=5){
log(“firstValue:+firstValue”);
console.log(“secondValue:+secondValue”);
console.log(“--------------”;
}
试验(2,3);
试验(2)代码>当您这样做时
test(2, secondValue = 3)
实际上,您正在这样做:
secondValue = 3
test(2, 3)
第一部分(secondValue=3
)创建了一个名为secondValue
的全局变量,这要归功于。*它与函数中的secondValue
参数无关。JavaScript没有命名参数。(例如,在调用时不能说“这是secondValue
”的值,除非将其放在参数列表中的正确位置。如果要在调用时指定名称,可以使用destructuring as,它不是真正命名的参数,但可以达到相同的目的。)
第二部分(将3
传递到test
)发生的原因是赋值的结果是赋值的值(因此secondValue=3
将secondValue
设置为3
,并生成值3
,然后传递到test
)
要使用3
调用test
以获得secondValue
,只需这样做。例如:
test(2, 3);
如果要禁用第二个选项,将使用默认选项:
test(2);
例如:
功能测试(第一个值,第二个值=5){
log(“firstValue:+firstValue”);
console.log(“secondValue:+secondValue”);
console.log(“--------------”;
}
试验(2,3);
试验(2)
secondValue=3只是常规赋值。实际上,这只是测试(3)@Joe:创建(我的博客文章)的副作用。参数的默认初始值设定项与JavaScript中不存在的命名参数没有任何关系。我想,您想将默认参数与解构结合起来,在您的示例中,您刚刚创建了一个需要2个参数的方法,并为它们提供了默认值。secondValue=3只是常规赋值。实际上,这只是测试(3)@Joe:创建(我的博客文章)的副作用。参数的默认初始值设定项与JavaScript中不存在的命名参数没有任何关系。我想,您想将默认参数与解构结合起来,在您的示例中,您刚刚创建了一个需要2个参数的方法,并为它们提供了默认值。好的一点是,OP可能正在寻找命名参数(而不是仅仅对如何使用默认值感到困惑)。如果是这样的话,破坏结构可能是一个挑战