将一个对象添加到另一个对象中的另一个对象中
假设有三个对象,一个注释、一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你为什么不简单地
Why don't you simply
是的,您可以这样做,并且不必使用扩展,只需设置属性即可。您甚至不必使用不同的名称,因此以下操作就可以了:
然后,正如您所写,您可以使用它
来检索嵌套属性。
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 :
Then, just as you wrote, you could use
to retrieve the nested properties.
我想您还可以使用 prototype 属性来链接您的对象
I guess you could also use the prototype property to link your objects