gpt4 book ai didi

JavaScript 对象变量?

转载 作者:行者123 更新时间:2023-12-03 11:32:06 24 4
gpt4 key购买 nike

我有以下 JS 结构:

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

是否可以制作 child_two等于 child值?

类似这样的:master.two.child ='#mydiv'

最佳答案

不,您必须分两步完成,如下所示:

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

关于JavaScript 对象变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26693020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com