[英]Can sombody explain me this arrow function in javascript?
here is the code:这是代码:
const createI18n = (config) => ({
locale: ref(config.locale),
messages: config.messages,
$t(key) {
return this.messages[this.locale.value][key]
},
})
Is locale
an object or a proprety of object? locale
是 object 还是 object 的财产?
I'm kind of new in javascript and i really struggling in understanding this arrow function.我是 javascript 的新手,我真的很难理解这个箭头 function。
This has nothing to do with arrow functions.这与箭头函数无关。
locale
is the name of a property of an object. locale
是 object 的属性名称。
The value of that property is another object (which has a property named value
on it).该属性的值是另一个 object(其上有一个名为
value
的属性)。
To better understand what is going on here, you could take a look at a normal function equivalent:为了更好地理解这里发生了什么,您可以查看一个普通的 function 等效项:
function createI18n(config){
return {
locale: ref(config.locale),
messages: config.messages,
$t(key) {
return this.messages[this.locale.value][key]
}
}
}
So basically you are returning an object from that arrow function.所以基本上你是从箭头 function 返回一个 object。 You are also using a shorthand version of an arrow function, which omitts the return keyword.
您还使用了箭头 function 的简写版本,它省略了 return 关键字。
So those two are equivalent to the one above as well所以这两个也相当于上面的一个
const createI18n = (config) => ({
locale: ref(config.locale),
messages: config.messages,
$t(key) {
return this.messages[this.locale.value][key]
},
})
const createI18n = (config) => {
/* you could do some operations here, but not in the shorthand version */
return {
locale: ref(config.locale),
messages: config.messages,
$t(key) {
return this.messages[this.locale.value][key]
},
}
}
It is just a more convenient kind to write the function.写function只是一种更方便的方式。
update: The result object would look like this:更新:结果 object 看起来像这样:
{
locale: //some value,
messages: //some other value,
$t: // function
}
update 2: you could rewrite your function to observe the result in the console.更新 2:您可以重写您的 function 以在控制台中观察结果。
const createI18n = (config) => {
const result = {
locale: ref(config.locale),
messages: config.messages,
$t(key) {
return this.messages[this.locale.value][key]
},
}
console.log(result);
return result
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.