访问不带点和一对括号的变量(parent.child.grandchild)的孙。
|
我正在使用一种转换表来构建与画布相关的类。转换表可以由用户编辑。 (不是很重要,但是也许您想知道为什么):
cLayout = function(option) {
//obtaining the canvas (el) here
this.setup = function(option) {
t=this.table;
for (var p in t)
{
el[t[p][0]] = option[p]||t[p][1]
}
}
this.setup(option)
}
cLayout.prototype.table = {
width:[[\'style\'][\'width\'],\"100%\"],
height:[\'style\'[\'height\'],\"100%\"],
bg:[[\'style\'][\'backgroundColor\'],\"\"],
position:[[\'style\'][\'position\'],\"absolute\"],
left:[\'style\'[\'left\'],\"0px\"],
top:[\'style\'[\'left\'],\"0px\"]
}
例:
var b = new cLayout({left:\'10%\',width:\'90%\'})
实际问题:
通常,我会使用ѭ2来设置el.style.width。
但是我想在没有第二对括号的情况下使用el[something]
:我希望属性名称是完全可变的(我也希望能够设置el[\'innerHTML\']
)。因此,有没有办法通过使用a[b]
来获得孙子,而无需使用a[b][c]
?
附言当然,我不想使用eval。
没有找到相关结果
已邀请:
1 个回复
惜堡沁戚
的字符串并设置相应的属性:
(您还应该测试某个属性是否可用。) 然后您的代码可能如下所示: