将一个对象添加到另一个对象中的另一个对象中

发布于 2025-01-06 02:42:44 字数 458 浏览 1 评论 0原文

假设有三个对象,一个注释、一个 noteState 和历史记录:

var note = {
    text: "hello new world"
}

var history = {}
var noteState = {}

我如何才能将注释添加到 noteState 中,然后将 noteState 添加到历史记录中?

我是否可以做类似的事情:

$.extend(noteState, {"note1": note});
$.extend(history, {"noteState1": noteState});

如果我要参考它以从注释中获取文本,我将如何去做?

history.noteState1.note1.text  // ?

或者还有其他方法可以解决这个问题吗?谢谢你!

Say that there are three objects, a note, a noteState, and history:

var note = {
    text: "hello new world"
}

var history = {}
var noteState = {}

how would i be able to add note into noteState, and then add noteState into history?

could i potentially do something like:

$.extend(noteState, {"note1": note});
$.extend(history, {"noteState1": noteState});

and if I were to refer back to it to grab the text from note, how would i go about doing so?

history.noteState1.note1.text  // ?

or is there another method to go about this? thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

国产ˉ祖宗 2025-01-13 02:42:44

你为什么不简单地

var note = {
    text: "hello new world"
}

var history = {}
var noteState = {}

noteState.note = note;
history.noteState= noteState;
alert(history.noteState.note.text);//alerts hello new world

Why don't you simply

var note = {
    text: "hello new world"
}

var history = {}
var noteState = {}

noteState.note = note;
history.noteState= noteState;
alert(history.noteState.note.text);//alerts hello new world
街角迷惘 2025-01-13 02:42:44

是的,您可以这样做,并且不必使用扩展,只需设置属性即可。您甚至不必使用不同的名称,因此以下操作就可以了:

noteState.note = note;
history.noteState = noteState;

然后,正如您所写,您可以使用它

 history.noteState.note.text

来检索嵌套属性。

Yes, you can do that and you don't have to use extend, simply set the properties. You don't even have to use distinct names, so following will do just fine :

noteState.note = note;
history.noteState = noteState;

Then, just as you wrote, you could use

 history.noteState.note.text

to retrieve the nested properties.

殤城〤 2025-01-13 02:42:44

我想您还可以使用 prototype 属性来链接您的对象

I guess you could also use the prototype property to link your objects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文