开发者

JavaScript acrobatics: How to declare an array, elements, and properties in one string

开发者 https://www.devze.com 2023-01-24 09:31 出处:网络
Given the following code: eval(\'(mapping.\' + binding.field + \' = eval(\"extVar\") )\'); where binding.field = \"LPP开发者_Python百科[0].price\" (a string) and

Given the following code:

eval('(mapping.' + binding.field + ' = eval("extVar") )');

where binding.field = "LPP开发者_Python百科[0].price" (a string) and extVar is the same value of "LPP[0].price"

How could you program the statement above to end up with:

mapping.LPP[0].price = LPP[0].price;

when mapping.LPP isn't defined yet? This code cycles through the Microsoft JQuery templates pulling variable names as strings from the tempaltes. When we reference properties in arrays, it throws up based on this code. It says that mapping.LPP[0] isn't defined.


if(!mapping.LPP) mapping.LPP = [];
mapping.LPP[0].price = LPP[0].price;
0

精彩评论

暂无评论...
验证码 换一张
取 消