Chris
Chris

Reputation: 455

Javascript access parent from child

I'm trying to work out a way of the child class accessing the parent class when the parent will never know what children it has?

I am using Object.create to create my classes and right now I'm doing this. Note in the child class "obj.parent = parentClass"

There has to be a better way of doing this but I don't know.

var parentClass = (function() {
    var obj = Object.create({});

    var _parentProperty = 'foo';

    Object.defineProperties(obj,
        {
            'parentProperty': {
                configurable: false,
                enumerable: true,
                set: function(value) {
                    _parentProperty = value;
                },
                get: function() {
                    return _parentProperty;
                }
            },
            'parentMethod': {
                value: function() {
                    alert('Parent method called');
                },
                writable: false,
                configurable: false,
                enumerable: false
            }
        }
    );

    return obj;
})();

var childClass = (function() {
    var obj = Object.create(parentClass);
    obj.parent = parentClass;

    var _childProperty = 'bar';

    Object.defineProperties(obj,
        {
            'childProperty': {
                configurable: false,
                enumerable: true,
                set: function(value) {
                    _childProperty = value;
                },
                get: function() {
                    return _childProperty;
                }
            },
            'childMethod': {
                value: function() {
                    alert('Child method called');
                },
                writable: false,
                configurable: false,
                enumerable: false
            },
            'callParent': {
                value: function() {
                    alert(this.parent.parentProperty);
                    this.parent.parentMethod();
                },
                writable: false,
                configurable: false,
                enumerable: false
            }
        }
    );

    return obj;
});

var myClass = new childClass();
myClass.callParent();

Upvotes: 0

Views: 645

Answers (1)

Shyam
Shyam

Reputation: 792

Use return keywords in your both Object.defineProperties object constructor for both parentClass & child class

<script>
var parentClass = (function() {
    var obj = Object.create({});

    var _parentProperty = 'foo';

    return Object.defineProperties(obj,
        {
            'parentProperty': {
                configurable: false,
                enumerable: true,
                set: function(value) {
                    _parentProperty = value;
                },
                get: function() {
                    return _parentProperty;
                }
            },
            'parentMethod': {
                value: function() {
                    alert('Parent method called');
                },
                writable: false,
                configurable: false,
                enumerable: false
            }
        }
    );
})();


var childClass = (function() {
    var obj = Object.create(parentClass);
    obj.parent = parentClass

    var _childProperty = 'bar';

    return Object.defineProperties(obj,
        {
            'childProperty': {
                configurable: false,
                enumerable: true,
                set: function(value) {
                    _childProperty = value;
                },
                get: function() {
                    return _childProperty;
                }
            },
            'childMethod': {
                value: function() {
                    alert('Child method called');
                },
                writable: false,
                configurable: false,
                enumerable: false
            },
            'callParent': {
                value: function() {
                    alert(this.parent.parentProperty);
                    this.parent.parentMethod();
                },
                writable: false,
                configurable: false,
                enumerable: false
            }
        }
    );
});

var myClass = new childClass();
myClass.callParent();
</script>

Upvotes: 1

Related Questions