suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Javascript – js-Problem mit eckigen Klammern

import {INCREMENT} from "./types"

const mutations = {
    [INCREMENT] (state) {
      state.count++;
    }
}

[INCREMENT] INCREMENT ist eine Variable, die direkt verwendet werden kann. Warum müssen wir eine Klammer hinzufügen?

高洛峰高洛峰2871 Tage vor690

Antworte allen(2)Ich werde antworten

  • 黄舟

    黄舟2017-05-16 13:44:12

    [INCREMENT]是计算INCREMENT这个变量的值作为函数名,不使用中括号是把INCREMENT这个字符串作为函数名。

    const INCREMENT = 'myfunc';
    
    const mutations = {
        [INCREMENT] (state) {
          state.count++;
        }
    }
    

    相当于上面的代码,结果是

    const mutations = {
        myfunc(state) {
          state.count++;
        }
    }

    const INCREMENT = 'myfunc';
    const mutations = {
        INCREMENT (state) {
          state.count++;
        }
    }

    的结果是

    const mutations = {
        INCREMENT(state) {
          state.count++;
        }
    }

    Antwort
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 13:44:12

    这是 computed property names

    https://developer.mozilla.org...

    Antwort
    0
  • StornierenAntwort