0

In the second file below I am trying to figure out what [CALL_API] is. What do the brackets mean? Is [CALL_API] the key for the object or is [CALL_API] resolved to something that is the actual key? And what is the purpose of doing it this way instead of using the string "CALL_API" as the key?

// file: middlewares/api.js

export const CALL_API = Symbol('CALL_API');

// (rest of file ommitted)

-

import { CALL_API } from 'middlewares/api';

store.dispatch({
  [CALL_API]: {
    method: 'get',
    path: '/questions',
    sendingType: 'SENDING_QUESTIONS',
    successType: 'LOAD_QUESTIONS_SUCCESS',
  }
});

This code was from a react-redux app. More of the context can be seen is this pluker.

ken_o
  • 374
  • 2
  • 8
  • Symbols can be used as object keys. They don't resolve to strings. They're the only non-strings that can be used as keys. – SimpleJ Feb 01 '17 at 19:20
  • its a new es6 thing where you can use a var for the key, much like we already did for values. – dandavis Feb 01 '17 at 19:20
  • To clarify what needs answering - are you aware of exactly what `Symbol`s are? – James Thorpe Feb 01 '17 at 19:20
  • I'm confused about symbols and why they are combined with the brackets. The question was marked as a duplicate but I asked another because I don't understand how the brackets work specifically with symbols. And when I tried an experiment it worked better without the brackets: If I do `var bar = Symbol('bar')` then do `var foo = {[bar]: 99}`. When I try to use `foo.bar` it is undefined. But if I don't use the brackets I get 99. – ken_o Feb 02 '17 at 19:34

0 Answers0