使用常量作为Javascript关联数组的索引

Eda*_*aor 19 javascript associative-array constants

我想在JS中创建一个关联数组,但是使用定义为类的一部分的常量作为索引.

我想要这个的原因是这个类的用户可以使用常量(定义事件)来触发动作.

一些代码来说明:

STATE_NORMAL = 0;
STATE_NEW_TASK_ADDED = 0;
this.curr_state = STATE_NEW_TASK_ADDED;

this.state_machine = {
    /* Prototype:
    STATE_NAME: {
        EVENT_NAME: {
            "next_state": new_state_name,
            "action": func
        }
    }
    */

    STATE_NEW_TASK_ADDED : { // I'd like this to be a constant
        this.EVENT_NEW_TASK_ADDED_AJAX : {
            "next_state": STATE_NEW_TASK_ADDED,
            "action" : function() {console.log("new task added");},
        }
    }
}

// Public data members.
// These define the various events that can happen.
this.EVENT_NEW_TASK_ADDED_AJAX = 0;
this.EVENT_NEW_TASK_ADDED_AJAX = 1;
Run Code Online (Sandbox Code Playgroud)

我无法解决这个问题.我对JS不太好,但看起来无论我做什么,数组都是用字符串而不是常量定义的.有没有办法强制数组使用常量?

谢谢!

Ash*_*she 38

实际上,这里的问题是,当您按字面定义对象时,不能对关键部分使用值.

也就是说,这使用了预期的常量值:

var CONSTANT_A = 0, CONSTANT_B = 1;
var state_machine = {};
state_machine[CONSTANT_A] = "A";
state_machine[CONSTANT_B] = "B";
console.log(state_machine[0]); // => A
console.log(state_machine[1]); // => B
Run Code Online (Sandbox Code Playgroud)

但这不会按预期工作,而是使用字符串CONSTANT_A作为键:

var CONSTANT_A = 0, CONSTANT_B = 1;
var state_machine = {
    CONSTANT_A: "A",
    CONSTANT_B: "B",
};
console.log(state_machine[0]); // => undefined
console.log(state_machine["CONSTANT_A"]); // => A
console.log(state_machine.CONSTANT_A); // => A
Run Code Online (Sandbox Code Playgroud)

JavaScript有一个简写来定义对象文字,你可以省略键周围的双引号.表达式不能使用,因此CONSTANT_A不会被评估.

另见@ Kristian的答案:ES6 /现代JS,基本上可以实现你想要的.


Kri*_*ian 33

在ES6中,您可以使用对象键的计算值.

var CONSTANT_A = 0, CONSTANT_B = 1
var state_machine = {
    [CONSTANT_A]: function () {
        return 'a'
    },
    [CONSTANT_B]: function () {
        return 'b'
    }
};

console.log(state_machine)
Run Code Online (Sandbox Code Playgroud)

这在IE 11或Safari浏览器中不起作用:https: //kangax.github.io/compat-table/es6/#test-object_literal_extensions_computed_properties


Jul*_*nel 24

Let's say you have the following constants:

const COMPANIES = "companies";
const BRANCHES = "branches";
const QUEUES = "queues";
const LOGOUT = "logout";
Run Code Online (Sandbox Code Playgroud)

If you declare the dictionary this way:

var itemsToState = {
  COMPANIES: true,
  BRANCHES: false,
  QUEUES: false,
  LOGOUT: false,
}

// You will get:
// { COMPANIES: true, BRANCHES: false, QUEUES: false, LOGOUT: false }
Run Code Online (Sandbox Code Playgroud)

Note the keys are uppercase ^ because it is not using the constant's value.

If you want to use the constant's value as key, you need to do this:

var itemsToState = {
  [COMPANIES]: true,
  [BRANCHES]: false,
  [QUEUES]: false,
  [LOGOUT]: false,
}

// You will get:
// { companies: true, branches: false, queues: false, logout: false }
Run Code Online (Sandbox Code Playgroud)

Note the keys are lowercase ^ because it is using the constant's value.

  • 这些示例使代码和答案易于理解。谢谢! (4认同)