JSFiddle - React, Tailwind, and code Playground

by badsyntax

HTML

<h3>Overview</h3>

<p>Is it possible to list child object properties from a super object?</p>

JavaScript

var inherits=function(a,b){a.super_=b,a.prototype=Object.create(b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}})};

/* Our super object */
var MySuper = function () {
    this.superProp = 'hello';
};
MySuper.prototype.superMethod = function () {
    return 'test';
};
MySuper.prototype.getChildPropertiesFromSuper = function () {
    
    // The problem: We want to list all the child properties: properties that are not
    // found in the Super prototype, nor set as instance properties in 
    // the Super contructor.
    
    // Option 1: We cannot use Object.keys(this) as that will return names of
    // properties belonging to MySuper
    
    // Option 2: The following will return the combined properties of the 
    // Super and Sub objects
    //for(var key in this) {
    //    props.push(key);
    //}
    
    // Option 3: The following will only list properties of the MySuper object
    // (the same as using Object.keys)
    //for(var key in this) {
    //    if (this.hasOwnProperty(key)) {
    //       props.push(key);
    //    }
    //}
    
    // Option 4: Loop through all properties of this instance, and compare property names
    // to property names in the Super prototype. This does not account for instance properties
    // set in the Super constructor! Also, what about overriden properties?
    //for(var key in this) {
    //    if (typeof MySuper.prototype[key] !== 'undefined') {
    //     continue;   
    //    }
    //    props.push(key);
    //}
    
    //return 
    
    // Option 5: ???? 
    var props = [];
    for(var key in this) {
        if (MySuper.prototype.hasOwnProperty(key)) {
           props.push(key);
        }
    }
    return props;
};


/* Our child object which inherits from super */
var MyChild = function () {
    MySuper.apply(this, arguments);
    this.childProp = 'hello';
};
inherits(MyChild, MySuper);

MyChild.prototype.childMethod = function () {
    return 'child';
};

var child = new...