我看过诸如在 JavaScript中声明多个变量之类的帖子,这些帖子专注于唯一变量(以及很多变量),但我正在研究许多非唯一变量,例如我想做一个更大的版本......
var rep;
var state_def, state_MA, state_WY, state_TN = 'joeblow';
var state_AZ, state_TX = 'moefoe';
var state_MT, state_OR = 'growmo';
var state_OK, state_LA = 'billyjoe';
var state_WA, state_NM, state_MS = 'messytess';
var state_VA, state_NY = 'slickrick';
var state_CA = 'icecube';
//or
var state_def = state_MA = state_WY = state_TN = 'joeblow';
只是为了理解,我正在使用这样的开关......
switch (capitalise(data.regionName)) {
case 'Arizona': rep = state_AZ; break;
case 'Texas': rep = state_TX; break;
case 'Montana': rep = state_MT; break;
case 'Oregon': rep = state_OR; break;
case 'New Mexico': rep = state_NM; break;
case 'Mississippi': rep = state_MS; break;
case 'Oklahoma': rep = state_OK; break;
case 'Louisiana': rep = state_LA; break;
case 'Washington': rep = state_WA; break;
case 'Massachusetts': rep = state_MA; break;
case 'Wyoming': rep = state_WY; break;
case 'Tennessee': rep = state_TN; break;
case 'Virginia': rep = state_VA; break;
case 'Illinois': rep = state_IL; break;
case 'Nevada': rep = state_NV; break;
default: rep = state_def; break;
};
这也将有几千个邮政编码,因此需要一个辅助开关。这就是为什么需要尽可能短地声明状态(或 zip)变量/用户关联的原因。但我也不想陷入我可能没有意识到的任何陷阱。default: rep = state_def; break;
除了返回未定义外,这现在可以工作。此外,在 IE8 中不起作用。