我有以下JS结构:
var master = {
one: 'this is text',
two: [
{
child: '#mydiv'
child_two: ''
},
{
child: '#mydiv'
child_two: ''
},
]
};
是否可以使child_two
等于child
值?
这样的事情:master.two.child
= '#mydiv'
答案 0 :(得分:1)
不,你必须分两步完成,如下所示:
var master = {
one: 'this is text',
two: {
child: '#mydiv'
}
};
master.two.child_two = master.two.child;
alert(JSON.stringify(master, null, 4));
或者,您可以提前为相同的值定义变量,如下所示:
var myDivSelector = '#mydiv';
var master = {
one: 'this is text',
two: {
child: myDivSelector,
child_two: myDivSelector
}
};