0

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();
Chris
  • 435
  • 4
  • 11
  • [John Resig - Simple JavaScript Inhertiance](http://ejohn.org/blog/simple-javascript-inheritance/), [Is John Resig's Javascript inheritance snippet deprecated?](http://stackoverflow.com/questions/15050816/is-john-resigs-javascript-inheritance-snippet-deprecated) – Andreas Jan 07 '14 at 09:09
  • Thanks and an interesting read and I will be trying out some things from there but I'm not using John Resig's Javascript inheritance. – Chris Jan 07 '14 at 09:21
  • I have found that I can replace `obj.parent = parentClass;` with `obj.parent = obj.__proto__;` but that isn't realy any more useful. I'm looking for something to write into the parent class that will set the parent in the child class. – Chris Jan 07 '14 at 09:29
  • @thefourtheye Good point. I'm now wondering if thats a chrome thing. – Chris Jan 07 '14 at 09:34

1 Answers1

1

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>
Shyam
  • 782
  • 5
  • 12