JavaScript 访问父对象属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29734312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript access parent object attribute
提问by Jo Colina
I have a small issue in JS, I have two nested objects, and I would like to access a variable from the parent, like so:
我在 JS 中有一个小问题,我有两个嵌套对象,我想从父级访问一个变量,如下所示:
var parent = {
a : 5,
child: {
b : 3,
displayA : function(){
console.log(this.a);
//undefined
},
displayB : function(){
console.log(this.b);
//displays 3
}
}
}
And I would just like to know how to make parent.child.displayA work :) (I have sub-objects that need access to a parent's variable)
我只想知道如何让 parent.child.displayA 工作:)(我有需要访问父变量的子对象)
Any help appreciated Thank you very much!
任何帮助表示赞赏非常感谢!
回答by Oriol
You can use call
to set the value of this
:
您可以使用call
来设置值this
:
parent.child.displayA.call(parent); // 5
You may also be interested in bindingit:
您可能还对绑定它感兴趣:
parent.child.displayA = function(){
console.log(this.a);
}.bind(parent);
parent.child.displayA(); // 5
Or you you can just use parent
instead of this
:
或者您可以使用parent
代替this
:
parent.child.displayA = function(){
console.log(parent.a);
};
parent.child.displayA(); // 5
回答by Dmytro Medvid
You can use super.prop
to access parent class properties. Of course, only if you are using ES6.
您可以使用super.prop
访问父类属性。当然,前提是你使用的是 ES6。
回答by Falc
Javascript is prototype based, it is not a regular OOP language like PHP or Java.
Javascript 是基于原型的,它不像 PHP 或 Java 那样是常规的 OOP 语言。
Take a look to Inheritance and the prototype chainand implement something like Simple Javascript inheritance.
看一看继承和原型链,并实现类似简单的 Javascript 继承之类的东西。
You could probably access to parent through window.parent
if it is in the global scope, but your example will not work in every case.
window.parent
如果它在全局范围内,您可能可以通过它访问父级,但您的示例并非在所有情况下都有效。
回答by Matt Wetmore
There is no general way for the object child
to know that it is a member of the parent object. In your situation, you can make a direct reference to the parent object in displayA()
, like so:
没有通用的方法让对象child
知道它是父对象的成员。在您的情况下,您可以直接引用 中的父对象displayA()
,如下所示:
displayA : function(){
console.log(parent.a);
}
You do not need to place parent in the global scope and use window.parent
as another answer suggests; since you are declaring displayA
within the scope of parent
, the function will close over parent
, and it will be accessible anywhere within child
. Since the closure contains a reference to the parent
object, you will see that changes to parent
will be reflected in the behaviour of displayA
. For example, suppose parent
and child
are defined as in your example, except displayA
is modified to use parent.a
. Then:
您不需要将 parent 放在全局范围内并window.parent
按照另一个答案的建议使用;由于您displayA
在 范围内声明parent
,该函数将关闭parent
,并且可以在child
. 由于闭包包含对parent
对象的引用,您将看到对 的更改parent
将反映在 的行为中displayA
。例如,假设parent
和child
在您的示例中定义,除非displayA
修改为使用parent.a
。然后:
parent.child.displayA(); //=> 5
parent.a = 10;
parent.child.displayA(); //=> 10
All this being said, if you are trying to mimic OOP, then the other answer is right: you should read more about how the Javascript prototype chain works.
综上所述,如果您试图模仿 OOP,那么另一个答案是正确的:您应该阅读更多有关 Javascript 原型链如何工作的信息。
回答by Mostafa EL BARRAK
I think it doesn't really make sence to do it like that since you can access child object only through its parent. so why add a mthod displayB to the child while you can add it to the parent which has access to all child properties.
我认为这样做并没有什么意义,因为您只能通过其父对象访问子对象。那么为什么要向子项添加方法 displayB 而您可以将其添加到有权访问所有子属性的父项。
回答by tutankhamun
In your example you haven't inheritance. You may do this
在您的示例中,您没有继承。你可以这样做
...
displayA : function(){
console.log(parent.a);
// 5
},
...
parent.child.parent = parent;
parent.child.displayA();