简体   繁体   English

ES6默认参数:值分配不正确

[英]ES6 default parameters: values not assigned properly

I'm playing with ES6 Default parameters, and I got a weird behaviour. 我正在使用ES6默认参数,但行为异常。

Here is an short example of the problem: 这是问题的简短示例:

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

test(2, secondValue = 3)
test(secondValue = 3)

And its output: 及其输出:

firstValue: 2
secondValue: 3
----------
firstValue: 3
secondValue: 5
----------

In the second case, I expected firstValue: undefined and secondValue: 3 . 在第二种情况下,我希望firstValue: undefinedsecondValue: 3 Is this behaviour normal. 这是正常现象吗? Do I miss something? 我想念什么吗?

When you do 当你做

test(2, secondValue = 3)

you are, in effect, doing this: 您实际上是在这样做:

secondValue = 3
test(2, 3)

The first part ( secondValue = 3 ) creates a global variable called secondValue thanks to The Horror of Implicit Globals .* It has nothing to do with the secondValue parameter in your function. 第一部分( secondValue = 3 )由于隐式全局变量的恐怖而创建了一个名为secondValue的全局变量。*与函数中的secondValue参数无关。 JavaScript doesn't have named arguments. JavaScript没有命名参数。 (Eg, you can't say "here's the value for secondValue " when making a call except by putting it in the right position in the argument list. If you want to specify the names when making the call, you can use destructuring as cubbuk points out , which isn't really named arguments but can serve the same sort of purpose.) (例如,拨打电话时,您不能说“这是secondValue的值,除非将其放在参数列表中的正确位置。如果要在通话时指定名称,则可以将destructuring用作cubbuk指出 ,它不是真正命名的参数,但可以达到相同的目的。)

The second part (passing 3 into test ) happens because the result of an assignment is the value that was assigned (so secondValue = 3 sets secondValue to 3 and results in the value 3 , which is then passed into test ). 第二部分(将3传递给test )发生是因为赋值的结果是所赋值(所以secondValue = 3secondValue设置为3并产生值3 ,然后将其传递给test )。

To call test with 3 for secondValue , just do that. 要对secondValue3调用test ,只需执行该操作即可。 Eg: 例如:

test(2, 3);

If you want to leave the second one off, the default will be used: 如果要关闭第二个,将使用默认值:

test(2);

Example: 例:

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


* (that's a post on my anemic little blog) * (这是我贫乏的小博客上的帖子)

You are using a global variable without declaring it. 您正在使用全局变量而未声明它。 The assignment generates a global variable and does not affect the variables of function test . 该赋值生成一个全局变量,并且不影响功能test的变量。 These local variables are independent form the calling scope. 这些局部变量是独立于调用范围的。

 function test(firstValue, secondValue=5){ console.log("firstValue: " + firstValue); console.log("secondValue: " + secondValue); console.log("----------") } var secondValue; // with declaration console.log(secondValue); test(2, secondValue = 3) test(secondValue = 3) console.log(secondValue); 

You want to do destructuring I guess: 我想您想进行销毁:

 function test({firstValue, secondValue = 5} = {}){ console.log("firstValue: " + firstValue); console.log("secondValue: " + secondValue); console.log("----------") } test() // prints firstValue: undefined, secondValue: 5 test({}) // prints firstValue: undefined, secondValue: 5 test({firstValue: 2}) // prints firstValue: 2, secondValue: 5 test({secondValue: 3}) // prints firstValue: undefined, secondValue: 3 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM