javascript inherit object code example

Example: javascript inherit function

function inherit(c, p) {
        Object.defineProperty(c, 'prototype', {
            value: Object.assign(c.prototype, p.prototype),
            writable: true,
            enumerable: false
        });

        Object.defineProperty(c.prototype, 'constructor', {
            value: c,
            writable: true,
            enumerable: false
        });
    }
// Or if you want multiple inheritance

function _inherit(c, ...p) {
        p.forEach(item => {
            Object.defineProperty(c, 'prototype', {
                value: Object.assign(c.prototype, item.prototype),
                writable: true,
                enumerable: false
            });
        })

        Object.defineProperty(c.prototype, 'constructor', {
            value: c,
            writable: true,
            enumerable: false
        });
    }

Tags:

Misc Example