简体   繁体   中英

How can access static object parent member?

I have a object as myObject, I want in the myObject.bcc3 function access parents member,

the following is my example:

var myObject = {
    a: {
        a1: 1,
        a2: 2
    },
    b: {
        b1: 3,
        b2: 4,
        c: {
            c1: 5,
            c2: 6,
            c3: function() {
                //how can here access a.a1, a.a2, b.b1 and b.b2,??
                //not use myObject.a.a1, myObject.a.a2, myObject.b.b1....etc
                //can like to use this.parent.parent.a.a1??
            }
        }
    }
}

any idea?

Why not change how you construct your object?

function getObject() {
    var result = {
        a: {
           a1: 1,
           a2: 2
        },
        b: {
           b1: 3,
           b2: 4,
           c: {
               c1: 5,
               c2: 6
           }
        }
     };

    result.b.c.c3 = (function(parent) {
        var myA1 = parent.a.a1;
        var myA2 = parent.a.a2;
        var myB1 = parent.b.b1;
        var myB2 = parent.b.b2;
        //and so on
    })(result);

    return result;
}

And then var myObject = getObject();

The c3 has created a closure to the object that was being constructed. It has full access to a.a1, a.a2, etc.

var myObject = {
    a: {
        a1: 1,
        a2: 2
    },
    b: {
        b1: 3,
        b2: 4,
        c: {
            c1: 5,
            c2: 6,
            c3: function() {
                alert("this.parent.parent.a.a1 is " + this.parent.parent.a.a1);
            }
        }
    },
    init: function(obj, flag) {
        if (!obj) obj = this;
        for (var prop in obj) {
            if (typeof obj[prop] == "object" && prop != "parent") {
                obj[prop].parent = obj;
                arguments.callee(obj[prop], true);
            }
        }
        if (!flag) {
            delete this.init;
            return this;
        }
    }
}.init();

Then you have to call myObject.bcc3();
Done.

The interior of the function must call the other object properties as static identifiers and cannot refer to them in a relative means. This is subject to change, however, if you create methods to walk the properties of an object literal and bind these methods as properties of the Object prototype.

The usual strategy in these cases is to use closures with the module pattern and use the scope chain for the relationship.

An object doesn't know, nor are there methods for it to discover, the structure that created it. There are numerous ways to create an object structure and a single object can belong to many structures, so the concept of a "parent" is unreliable.

Using this is not a solution since a function's this is set by the call, you can't set it by how the function is created (except with ES5 bind ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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