JavaScript access parent object attribute

前端 未结 6 1886
臣服心动
臣服心动 2020-12-31 17:16

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:

var parent = {
    a : 5,

    child:          


        
6条回答
  •  醉酒成梦
    2020-12-31 17:33

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

提交回复
热议问题