且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Javascript中使用字符串作为变量名?

更新时间:2023-01-19 12:04:49

停止。重新组织您的代码。如果要选择带变量的变量,则必须为它们进行逻辑分组。说清楚。

Stop. Reorganise your code. If you want to select variables with a variable, then there has to be a logical grouping for them. Make it explicit.

var chat = {
    "1": "one",
    "2": "two"
};
var id = 1;
var new_is_a_keyword_and_cant_be_an_identifier = chat[id];