2016-09-20 5 views
4

私はちょうど新しい仕事を始めました。維持するいくつかのリアクションコードがあります。authenticationHandlers.jsというファイルがあります。私が遭遇したこのjavascriptの構文に慣れていない

const events = require("./authenticationEvents.js"); 

const authenticationHandlers = { 
    [events.Errored.Name](prev, event) { 
     const update = { 
      UnauthorizedError: event.Error 
     }; 

     return Object.assign({}, prev, update); 
    }, 
    [events.ClearError.Name](prev, event) { 
     const update = { 
      UnauthorizedError: null 
     }; 

     return Object.assign({}, prev, update); 
    } 
}; 

module.exports = authenticationHandlers; 

ブラケットが何を意味するのですか?私は本当にコードの機能についてのご質問はありませんが、ブラケットの構文は、言い換えればライン[events.Erorred.Name][events.ClearError.Name]

で何をするのか

+1

イベントオブジェクトからの名前を持つ2つの関数を定義しようとしているようです – klikas

答えて

5

プロパティ名として変数を使用できるように、それは次のとおりです。たとえば

const a = 'banana'; 
 
const fnName = 'pudding'; 
 

 
const b = { 
 
    [a]: 42, 
 
    [fnName]() { 
 
     console.log(`I am logging from ${fnName}`); 
 
    } 
 
}; 
 

 
console.log(b); //{banana: 42, pudding: fn} 
 
b[fnName]();

関連する問題