[英]javascript arrow function ()=>() meaning?
I came across this syntax. 我遇到了这种语法。 Could any one explain what
getArg1ListInfo:()=>(object.freeze(arg1))
means 任何人都可以解释
getArg1ListInfo:()=>(object.freeze(arg1))
意思
function foo (arg1,arg2) {
let retval = {
service:service
}
if(arg1) {
retval.getArg1InfoHelper = () => {
return {
subArg : "string",
getArg1ListInfo:()=>(object.freeze(arg1))
}
}
}
return retval
}
()=>(object.freeze(arg1))
is equivalent to 相当于
()=>object.freeze(arg1)
The brackets seem to be confusing you. 方括号似乎使您感到困惑。 They are not syntax, they are just redundant.
它们不是语法,只是多余的。
A reason one might use brackets in this way is for returning object literals. 可能以这种方式使用方括号的原因是为了返回对象文字。 To use @Phil's example from the comments,
要在评论中使用@Phil的示例,
() => ({foo: 'bar'})
In this example, the brackets are used to avoid a syntax error. 在此示例中,方括号用于避免语法错误。 It's possible that the person who wrote your example code has come against this problem in the past, and has formed a habit.
编写您的示例代码的人过去可能会遇到此问题,并养成了习惯。
The ()=>()
syntax is a shorthand way of defining a single-statement function, where the result of that statement is returned as the result of the arrow function. ()=>()
语法是定义单语句函数的一种简便方法,该语句的结果作为箭头函数的结果返回。
In the case of getArg1ListInfo:()=>(Object.freeze(arg1))
, when getArg1ListInfo
gets called, it will return the result of object.freeze(arg1)
. 对于
getArg1ListInfo:()=>(Object.freeze(arg1))
,当getArg1ListInfo
时,它将返回object.freeze(arg1)
的结果。
So for example: 因此,例如:
var result = foo({},{}).getArg1InfoHelper().getArg1ListInfo()
Will mean that result
contain the result of object.freeze(arg1)
. 这将意味着
result
包含object.freeze(arg1)
的结果。 According to the MDN docs , this means that result
would contain the object arg1
. 根据MDN文档 ,这意味着
result
将包含对象arg1
。
(params) => (expression)
is the equivalent of 等于
function (params) { return expression;}
source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions 来源: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.