Skip to content Skip to sidebar Skip to footer

Is It Possible To Implicitly Reference A Member Of The Parent Object From Within A Function Of A Child Object?

Is there a way, without explicitly calling the parent object by its instance name, to refer to the parent object's members? In the below example, the statement this.me refers to t

Solution 1:

I don't think there's a built-in way to do this, but you could just create a reference to the parent in your object definition:

var obj = {
    me: 'obj'
};

obj.child = {
    me: 'child',
    parent: obj,
    init: function() {
        var p = document.getElementById('console');
        var who = this.parent.me;
        p.innerHTML = who;
    }
};

Post a Comment for "Is It Possible To Implicitly Reference A Member Of The Parent Object From Within A Function Of A Child Object?"