简体   繁体   English

看起来像Array函数的JavaScript语法:[variable]({key},arg){}

[英]JavaScript syntax which looks like Array function: [variable] ({ key }, arg) { }

I was ask to study the code of employer, In the code, the employer did something like this: 我被要求研究雇主的代码,在代码中,雇主做了这样的事情:

export const actions = {
  [ACTIONS.ITEM_LIST.LOAD.name] ({commit}, payload) {
    const type = payload

Now, In this, I am unable to rectify this line of code 现在,在这里,我无法纠正这一行代码

 [ACTIONS.ITEM_LIST.LOAD.name] ({commit}, payload) {

Like is it a function or what? 喜欢它是一个功能还是什么? Can someone explain me the above syntax? 有人能解释一下上面的语法吗?

New-ish JavaScript allows property names in object literal expressions (what that { } block initializing actions is called) to compute property names from expressions by allowing [ ] for property names instead of identifiers or string constants as in the past. New-ish JavaScript允许对象文字表达式中的属性名称( { }阻止初始化actions被调用)通过允许[ ]获取属性名称而不是过去的标识符或字符串常量来计算表达式中的属性名称。

So what that means is that ACTIONS.ITEM_LIST.LOAD.name should be evaluated, and the string value of whatever that ends up being is used as the name of the function property of the object. 那么这意味着应该评估ACTIONS.ITEM_LIST.LOAD.name ,并将最终结果的字符串值用作对象的函数属性的名称。 (That too is a new-ish feature of the language; formerly properties had to be name : value strictly). (这也是该语言的新功能;以前的属性必须是name : value严格限定name : value )。

Now inside the formal parameter list to that function, {commit} is a destructuring formal parameter. 现在在该函数的形式参数列表中, {commit}是一个解构形式参数。 What it means is that the function expects that the first argument will be an object, and so inside the function the parameter (variable) commit should be bound to the value of that object's "commit" property (or undefined if there is no such property). 这意味着函数期望第一个参数是一个对象,因此在函数内部,参数(变量) commit应绑定到该对象的“commit”属性的值(如果没有这样的属性,则为undefined )。

So if we assume for example purposes that ACTIONS.ITEM_LIST.LOAD.name evaluates to the string "xyz", then one would be able to call: 因此,如果我们假设ACTIONS.ITEM_LIST.LOAD.name计算结果为字符串“xyz”,则可以调用:

var result = actions.xyz({ foo: "bar", commit: "everything" }, somePayload);

and in the function the string "everything" would be the value of the commit parameter. 在函数中,字符串“everything”将是commit参数的值。

It uses object method syntax in combination with computed property names and destructuring in the parameter list. 它使用对象方法语法结合计算属性名称和参数列表中的解构

ACTIONS.ITEM_LIST.LOAD presumably evaluates to an object: accessing the .name property of the object probably evaluates to a string. ACTIONS.ITEM_LIST.LOAD可能会计算到一个对象:访问对象的.name属性可能会计算为一个字符串。 For example, if the string is "foo", then the line there is equivalent to: 例如,如果字符串是“foo”,则那里的行相当于:

const actions = {
  'foo': function(arg1, payload) {
    let commit = arg1.commit; // using "let" because parameters can be reassigned
    const type = payload
    // ...
};
export actions;

(Technically, it's not entirely equivalent , but it nearly is - object methods can't be used as constructors.) (从技术上讲,它并不完全等效 ,但它几乎是 - 对象方法不能用作构造函数。)

[ACTIONS.ITEM_LIST.LOAD.name] ({commit}, payload) { [ACTIONS.ITEM_LIST.LOAD.name]({commit},payload){

will resolve to function declaration syntax: 将解析函数声明语法:

functionName (arguments) { statements } functionName(arguments){statements}

There are a couple of things playing into action here. 这里有几件事情在起作用。 First, actions is an object. 首先, 行动是一个对象。 Next, it uses new computed property name syntax. 接下来,它使用新的计算属性名称语法。 ie [ACTIONS.ITEM_LIST.LOAD.name] will provide functionName part of our syntax. [ACTIONS.ITEM_LIST.LOAD.name]将提供我们语法的functionName部分。 Note, that this is not an array, it is a computed property which will also be a key of our actions dictionary. 注意,这不是一个数组,它是一个计算属性 ,也是我们的动作字典的一个关键。 Lastly, it uses Shorthand method names syntax to create a function in the object actions without writing it as "key: value" notation. 最后,它使用速记方法名称语法在对象操作中创建函数,而不将其写为“key:value”表示法。 This too is new in ECMAScript2015. 这也是ECMAScript2015中的新功能。

This link here will give you clear idea if I don't clear all your doubts. 此链接在这里会给你明确的想法,如果我不清除所有的疑虑。

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

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