简体   繁体   English

我们何时必须在ES6箭头函数中使用()和{}

[英]When do we have to use () and {} in ES6 Arrow Functions

I'm little confused with the usage of parenthesis and flower brackets interchangeably in arrow functions. 我对箭头功能中可互换使用的括号和花括号感到困惑。

For instance, 例如,

Use of Flower brackets 花括号的使用

const sayHello = () => { console.log('Hello') }

Use of Parentheses 括号的使用

const sayHello = () => ( console.log('Hello') )

Both gives same output on the console. 两者在控制台上都提供相同的输出。 Do they both mean same? 两者的意思都一样吗? Sorry if this question sounds dumb. 抱歉,这个问题听起来很愚蠢。

When you use curly braces (flower brackets) it defines the function body - so you can place multiple semicolon-separated statements inside them. 当使用花括号(花括号)时,它定义了函数主体-因此您可以在其中放置多个用分号分隔的语句。 If you wish to return a value from the function, you must explicitly use the return statement. 如果希望从函数返回值,则必须显式使用return语句。

If you use parentheses, you are going to be returning the value of the expression inside the parentheses, and there can only be one expression (with no semicolon). 如果使用括号,则将在括号内返回表达式的值,并且只能有一个表达式(无分号)。 If you wish to do multiple things with parentheses, you can use the comma operator: 如果您希望用括号做很多事情,可以使用逗号运算符:

 const doTwoThings = () => (console.log("Hello"), console.log("Goodbye")); console.log(doTwoThings()); 

Here, you're logging Hello , then Goodbye , then you're logging the return value of the function - which is undefined . 在这里,您要记录Hello ,然后Goodbye ,再记录函数的返回值- undefined

You can do the same thing inside the curly braces, and you won't see any visible difference: 您可以在花括号内执行相同的操作,并且看不到任何明显的区别:

 const doTwoThings = () => {console.log("Hello"), console.log("Goodbye")}; console.log(doTwoThings()); 

Again you're logging Hello and Goodbye to the console, then you're logging the return value of the function - however, using {} needs to have the explicit return keyword, so it's undefined because we don't return anything from the function. 再次将HelloGoodbye记录到控制台,然后记录该函数的返回值-但是,使用{}需要具有显式的return关键字,因此它是undefined因为我们不从该函数返回任何内容。

Also note that sometimes you'll see both parentheses and curly braces: 另请注意,有时您会同时看到括号花括号:

 const aFunction = () => ({ foo: "bar" }); console.log(aFunction()); 

There's a nice explanation here , but basically when you have a function returning an object, the curly braces {} denote the object, and the parentheses enforce the implicit return of the contained expression - the object. 有一个很好的解释在这里 ,但基本上,当你有一个函数返回一个对象时,大括号{}表示对象,而括号执行包含表达的隐性收益-对象。 So the object is created, then the expression is evaluated and returned. 因此创建了对象,然后对表达式求值并返回。

When you use "flower brackets" as you put it, it's like you're defining an ordinary function, having multiple statements as usual. 当您使用“花括号”时,就像定义了一个普通函数一样,它具有多个语句。 In your example, it runs console.log('Hello') and doesn't return anything. 在您的示例中,它运行console.log('Hello')并且不返回任何内容。 When you use parentheses, you can specify only a single expression, and then the result of that expression becomes the return value. 使用括号时,只能指定一个表达式,然后该表达式的结果将成为返回值。 In other words, => (bar) is basically the same as => { return bar; } 换句话说, => (bar)=> { return bar; }基本上相同=> { return bar; } => { return bar; } . => { return bar; } Both work the same in your case since you're only using that function for its side effects and don't care about its return value. 两种情况在您的情况下都是一样的,因为您仅出于该功能的副作用使用该功能,而不必关心其返回值。

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

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