简体   繁体   English

箭头函数中的JavaScript Spread运算符

[英]JavaScript spread operator in arrow functions

This snippet of JavaScript code alerts 1 as answer. 此JavaScript代码段将1作为答案。 Can anyone please explain how this code executes? 谁能解释这个代码如何执行?

const b = [1,2,3];
const f = (a, ...b) => a+b;

alert( f( 1 ) );

There are a couple of things going on here. 这里发生了几件事。 The main one is that you're shadowing b , so the b outside the function isn't used within it. 最主要的是您要遮蔽 b ,因此函数内的b不在其中使用。 Instead, within it, you're creating a new array (because you've used a rest parameter , ...b ) and assigning it to the b parameter. 相反,您将在其中创建一个新数组(因为使用了rest参数 ...b )并将其分配给b参数。 Since you call f with just one parameter, that array is empty. 由于仅使用一个参数调用f ,因此该数组为空。 1+[] is "1" because when either of the operands to + isn't a primitive (arrays aren't primitives), it's coerced to a primitive, and coercing an array to a primitive (indirectly) results in doing a .join(",") on the array. 1+[]"1"原因是,当+的两个操作数都不是基元(数组不是基元)时,它被强制为基元,而将数组强制为基元(间接)会导致执行.join(",")在数组上。 With a blank array, .join(",") is "" . 对于空白数组, .join(",")"" Then, since one of the operands is a string, the other operand ( 1 ) is coerced to string ( "1") and it does "1"+"" which is, of course, "1" . 然后,由于其中一个操作数是字符串,因此另一个操作数( 1 )被强制转换为字符串( "1") ,并执行"1"+""当然是"1" (Details on that last bit in the spec .) (有关规范最后部分的详细信息。)

f(1) is the same as 1 + [] f(1)等于1 + []

f(1,2,3) is the same as 1 + [2, 3] f(1,2,3)1 + [2, 3]

That's all... 就这样...

The first line const b = [1,2,3]; 第一行const b = [1,2,3]; is not used because the b in the lambda expression is the argument, not the constant declared before. 之所以不使用,是因为lambda表达式中的b是参数,而不是之前声明的常量。

You can reference variables in a function call , however, when you define a function expression, the parameters name do not refer to any variables. 您可以在函数调用中引用变量,但是,在定义函数表达式时,参数名称不会引用任何变量。

You'll get the expected result if you call the function like this: 如果像这样调用该函数,您将获得预期的结果:

alert(f(1, b));

It takes the rest parameters ... as an array b . 它将其余参数...作为数组b

While this is empty, it is converted to an empty string and both operands are treated as string, because if one is a string, then it adds all values as string. 当它为空时,它将转换为空字符串,并且两个操作数都被视为字符串,因为如果一个是字符串,则它将所有值相加为字符串。

The result is '1' . 结果为'1'

 const b = [1, 2, 3]; const f = (a, ...b) => a + ''; console.log(typeof f(1)); 

Reproducing this in my browser's development tools console looks like this: 在我的浏览器的开发工具控制台中重现此内容,如下所示:

> b = [1,2,3]
> f = (a, ...b) => a+b
> f(1)
< "1"
// so it results in the string 1, why's that?
// lets try logging what a and b are in the function
> g = (a, ...b) => console.log("a=%o, b=%o", a, b)
> g(1)
< a=1, b=[]
// ah, so b is an array, which is what the spread operator does 
// (gathers all remaining arguments into an array, so the question 
// is then what does JavaScript return for the number 1 added to an empty array?
> 1 + []
< "1"

This behavior is one of the many quirks of JavaScript when using the + operator on different types. 在不同类型上使用+运算符时,此行为是JavaScript的许多怪癖之一

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

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