Javascript对象:获取父级 [英] Javascript objects: get parent

查看:180
本文介绍了Javascript对象:获取父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下(嵌套)对象:

I have the following (nested) object:

obj: { subObj: { foo: 'hello world' } };

我接下来要做的就是像这样引用子对象:

Next thing I do is to reference the subobject like this:

var s = obj.subObj;

现在我想做的是获取对象的引用obj 超出变量 s
类似于:

Now what I would like to do is to get a reference to the object obj out of the variable s. Something like:

var o = s.parent;

这有可能吗?

推荐答案

没有。无法知道它来自哪个对象。

No. There is no way of knowing which object it came from.

s obj。 subObj 两者都只引用同一个对象。

s and obj.subObj both simply have references to the same object.

你也可以这样做:

var obj = { subObj: {foo: 'hello world'} };
var obj2 = {};
obj2.subObj = obj.subObj;
var s = obj.subObj;

您现在有三个参考, obj.subObj obj2.subObj ,以及 s ,到同一个对象。它们都不是特别的。

You now have three references, obj.subObj, obj2.subObj, and s, to the same object. None of them is special.

这篇关于Javascript对象:获取父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆