JSFiddle - React, Tailwind, and code Playground

by Andrew Dunai

HTML

<script src="https://raw.githubusercontent.com/and3rson/classjs/master/class.js?1"></script>
<pre class="log"></pre>

JavaScript

console.log = function() {
    document.querySelector('.log').innerHTML += Array.prototype.slice.call(arguments).join(' ') + '<br />';
};

var Animal = new Class({
    $name: 'Animal',

    name: 'Unknown',
    age: 0,
    
    $constructor: function(name, age) {
        this.name = name;
        this.age = age;
    },
    
    getName: function() {
        return 'An animal named ' + this.name;
    }
});

var Cat = new Class(Animal, {
    $name: 'Cat',
    
    getName: function() {
        return 'A cat named ' + this.name;
    }
});

var Dog = new Class(Animal, {
    $name: 'Dog',
    
    aggression: null,
    
    $constructor: function(name, age, aggression) {
        this.parent.$constructor.call(this, name, age);
        this.aggression = aggression;
    },
    
    getName: function() {
        return (
            this.aggression ? 'An aggressive dog' : 'A dog'
        ) + ' named ' + this.name;
    }

});

var cat1 = new Cat('Joey', 5);
var cat2 = new Cat('Mandriva', 3);
var dog1 = new Dog('Bob', 8, false);
var dog2 = new Dog('Mary', 6, true);
var turtle = new Animal('Zoey', 42);

console.log(cat1.getName());
console.log(cat2.getName());
console.log(dog1.getName());
console.log(dog2.getName());
console.log(turtle.getName());

console.log('cat1 instanceof Cat =', cat1 instanceof Cat);
console.log('cat1 instanceof Animal =', cat1 instanceof Animal);
console.log('cat1 instanceof Dog =', cat1 instanceof Dog);
console.log('turtle instanceof Animal =', turtle instanceof Animal);
console.log('Dog instanceof Class =', Dog.class instanceof Animal);

console.log('cat1.$chain() =', cat1.$chain());
console.log('cat1.$dump():');
cat1.$dump();