简体   繁体   English

JavaScript访问父对象属性

[英]JavaScript access parent object attribute

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! 任何帮助表示非常感谢!

You can use call to set the value of this : 您可以使用call来设置的值this

parent.child.displayA.call(parent); // 5

You may also be interested in binding it: 您可能也有兴趣绑定它:

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

You can use super.prop to access parent class properties. 您可以使用super.prop访问父类属性。 Of course, only if you are using ES6. 当然,只有你使用的是ES6。

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 chain and 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访问父级,但是您的示例在每种情况下都不起作用。

There is no general way for the object child to know that it is a member of the parent object. 对象child没有通用的方法知道它是父对象的成员。 In your situation, you can make a direct reference to the parent object in displayA() , like so: 在您的情况下,您可以直接引用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; 您不需要将父项放在全局范围内,并使用window.parent作为另一个答案建议; since you are declaring displayA within the scope of parent , the function will close over parent , and it will be accessible anywhere within child . 因为您在parent范围内声明displayA ,所以该函数将关闭parent ,并且可以在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 . 由于封包含对一个参考parent的对象,你会看到,以改变parent会的行为来体现displayA For example, suppose parent and child are defined as in your example, except displayA is modified to use parent.a . 例如,假设parentchild在您的示例中定义,除了displayA被修改为使用parent.a Then: 然后:

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原型链如何工作的内容。

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. 那么为什么要添加一个mthod displayB给孩子,同时你可以将它添加到有权访问所有子属性的父级。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM