Javascript对象变量?

时间:2014-11-01 20:41:52

标签: javascript

我有以下JS结构:

var master = {
    one: 'this is text',
    two: [
        {
            child: '#mydiv'
            child_two: ''
        },
        {
            child: '#mydiv'
            child_two: ''
        },
    ]
};

是否可以使child_two等于child值?

这样的事情:master.two.child = '#mydiv'

1 个答案:

答案 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
    }
};