[英]Function works withouth curly braces and doesn't work with them, why?
I'm doing FreeCode camp exercises, and this has messed-up my mind:我正在做 FreeCode 训练营的练习,这让我很困惑:
const squareList = arr =>
arr
.filter(num => num > 0 && num % parseInt(num) === 0)
.map(num => Math.pow(num, 2));
const squaredIntegers = squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]);
console.log(squaredIntegers);
This works correctly, but if i put:这可以正常工作,但是如果我输入:
const squareList = arr => {
arr
.filter(num => num > 0 && num % parseInt(num) === 0)
.map(num => Math.pow(num, 2));
}
const squaredIntegers = squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]);
console.log(squaredIntegers);
The typical curly brackets which are used normally in many functions, it doesn't work.在许多功能中通常使用的典型大括号,它不起作用。 Why?
为什么?
One is a set of statements
(with curly braces) and other is an expression
一个是一组
statements
(带花括号),另一个是expression
(param1, param2, …, paramN) => { statements } // needs to return something
(param1, param2, …, paramN) => expression
// equivalent to: => { return expression; }
So when you are using the first form,you need to return
something from it所以当你使用第一种形式时,你需要从中
return
一些东西
So in your non working case it should be a return
statement所以在你的非工作情况下,它应该是一个
return
声明
const squareList = arr => {
return arr
.filter(num => num > 0 && num % parseInt(num) === 0)
.map(num => Math.pow(num, 2));
}
Any unit of code that can be evaluated to a value is an expression .
任何可以计算为值的代码单元都是一个表达式。
A statement is an instruction or set of instructions to perform a specific action
语句是执行特定操作的指令或指令集
A more detailed explanation is here in mozilla docs更详细的解释在Mozilla 文档中
Without the curly brackets, return
is added for you.如果没有大括号,则会为您添加
return
。
With the curly brackets, you need to add it yourself.使用大括号,您需要自己添加。
const squareList = arr => {
return arr
.filter(num => num > 0 && num % parseInt(num) === 0)
.map(num => Math.pow(num, 2));
}
const squaredIntegers = squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]);
console.log(squaredIntegers);
will work correctly.将正常工作。
For better or for worse, javascript allows arrow functions to be either of the form无论好坏,javascript 允许箭头函数为以下任一形式
args => expression
or或者
args => function_body
So they "straddle the line" between old-school Javascript closures ( function (arglist...) {...}
) which must have a function body, and lambdas in more purely functional languages where they would always take an expression.因此,他们在老式 Javascript 闭包(
function (arglist...) {...}
)之间“跨越界限”,它们必须具有 ZC1C425268E68385D1AB5074C17A94F14 的主体,并且总是采用更多的纯函数式语言表达。
The working example you have is of the form args => expression
, but if you put curly-braces around the expression, it is interpreted as args => function_body
.您拥有的工作示例的形式为
args => expression
,但如果您在表达式周围加上花括号,它将被解释为args => function_body
。 But a function body with no return statement returns undefined
.但是没有 return 语句的 function 主体返回
undefined
。 This is easy to fix by adding the return
keyword, since args => expression
is really a shorthand for a function that returns what the expression evaluates to.这很容易通过添加
return
关键字来解决,因为args => expression
实际上是 function 的简写,它返回表达式的计算结果。
args => {return expression}
Note that this syntactic ambiguity also can lead to problems if you want an arrow function to whose expression
should be an object literal;请注意,如果您想要一个箭头 function 的
expression
应该是 object 文字,那么这种语法歧义也会导致问题; in that case you have to say在那种情况下,你必须说
args => ({
// object key/value pairs
})
(note the extra () which prevent the compiler from thinking you're providing a function body). (注意额外的 () 会阻止编译器认为您提供的是 function 主体)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.