JSFiddle - React, Tailwind, and code Playground

by Valera Siestov

HTML

<button id="button">Тест</button>

JavaScript

var jClass = function (Parent, properties) {
    var Child, F, i;

    // 1. Новый конструктор
    Child = function () {
        if (Child.superClass && Child.superClass.hasOwnProperty("init")) {
            Child.superClass.init.apply(this, arguments);
        }

        if (Child.prototype.hasOwnProperty("init")) {
            Child.prototype.init.apply(this, arguments);
        }
    };

    // 2. Наследование

    Parent = Parent || Object;
    F = function () {};
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.superClass = Parent.prototype;
    Child.prototype.constructor = Child;

    // 3. Добавить реализацию методов

    for (i in properties) {
        if (properties.hasOwnProperty(i)) {
            Child.prototype[i] = properties[i];
        }
    }

    return Child;
};

/**
 * Parent class
 */
var Phone = jClass(null, {
    init: function (variable) {
        console.log('constructor');
        this.name = variable;
    },
    getName: function () {
        return this.name;
    }
});

/**
 * Child class
 */
var Nokia = jClass(Phone, {
    init: function (variable) {
        console.log('Nokia constructor');
    },
    getName: function () {
        var name = Nokia.superClass.getName.call(this);
        return "I am Nokia " + name;
    }
});

var button = document.getElementById('button');

button.addEventListener('click', function () {
    var nokia = new Nokia("6600");
    alert(nokia.getName());
}, false);