简体   繁体   English

如何访问静态对象的父成员?

[英]How can access static object parent member?

I have a object as myObject, I want in the myObject.bcc3 function access parents member, 我有一个对象作为myObject,我想在myObject.bcc3函数中访问parents成员,

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(); 然后var myObject = getObject();

The c3 has created a closure to the object that was being constructed. c3为正在构造的对象创建了一个闭合。 It has full access to a.a1, a.a2, etc. 它具有对a.a1,a.a2等的完全访问权限。

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(); 然后,您必须调用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 ). 使用方法不是解决方案,因为函数的属性是通过调用设置的,因此无法通过函数的创建方式进行设置(ES5 bind除外)。

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

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