简体   繁体   English

子代的Java语言访问父级

[英]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. 我正在使用Object.create创建我的类,现在我正在这样做。 Note in the child class "obj.parent = parentClass" 注意子类“ 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();

Use return keywords in your both Object.defineProperties object constructor for both parentClass & child class 在您的Object.defineProperties对象构造函数中,对parentClass和child类都使用return关键字

<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>

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

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