我有类似的数组,它将包含更多元素。是否有更好/更紧凑的方式组织这个?
var codes= new Array();
codes['a']=0x0627;
codes['b']=0x0628;
codes['c']=0x0686;
codes['d']=0x062F;
codes['e']=0x0639;
codes['f']=0x0641;
codes['g']=0x06AF;
codes['h']=0x06BE;
codes['i']=0x06CC;
codes['j']=0x062C;
codes['k']=0x06A9;
codes['l']=0x0644;
codes['m']=0x0645;
codes['n']=0x0646;
codes['o']=0x06C1;
codes['p']=0x067E;
codes['q']=0x0642;
codes['r']=0x0631;
codes['s']=0x0633;
codes['t']=0x062A;
codes['u']=0x0626;
codes['v']=0x0637;
codes['w']=0x0648;
codes['x']=0x0634;
codes['y']=0x06D2;
codes['z']=0x0632;
codes['A']=0x0622;
codes['B']=0x0628;
codes['C']=0x062B;
codes['D']=0x0688;
codes['E']=0x0651;
codes['F']=0x064D;
codes['G']=0x063A;
codes['H']=0x062D;
codes['I']=0x0670;
codes['J']=0x0636;
codes['K']=0x062E;
codes['L']=0x0628;
codes['M']=0x064B;
codes['N']=0x06BA;
codes['O']=0x0628;
codes['P']=0x064F;
codes['Q']=0x0628;
codes['R']=0x0691;
codes['S']=0x0635;
codes['T']=0x0679;
codes['U']=0x0621;
codes['V']=0x0638;
codes['W']=0x0624;
codes['X']=0x0698;
codes['Z']=0x0630;
codes['>']=0x0650;
codes['<']=0x064E;
答案 0 :(得分:2)
JavaScript解析器必须遍历这些命令中的每一个,并为每个命令创建一个新的对象属性。相信我,我曾经试图以这种方式构建一个数组(当我刚开始使用JavaScript时)并且只需要30多秒就可以加载一个包含1,000个元素的数组。
相反,只需构建您的对象:
var codes = {
a: 0x0627,
b: 0x0628,
.......
};
这是一个单一的陈述,所以它会更快地运行。