JSFiddle - React, Tailwind, and code Playground

by J. Albert Bowden

JavaScript

console.log('Program starts');
// we start defining the Person class
function Person() {
    // this is a method of the class Person, it 'generates' a gender
    this.decideGender = function() {
        return (Math.random() > 0.5) ? 'boy' : 'girl';
    }
    // 'gender' is a property of the class
    this.gender = this.decideGender();
    // everything you execute directly here is the 'constructor', and it will execute as soon as you instantiate the class
    console.log('Person instantiated. Congratulations! It\'s a ' + this.gender + '!');
}
var person1 = new Person();
var person2 = new Person();
// we define now the Student class
function Student() {
    // 'decideSchool' is a method of the class Student
    this.decideSchool = function() {
        return (Math.random() > 0.5) ? 'Westside' : 'Eastside';
    }
    // here we call the constructor of the Parent class. Remeber this is the constructor of the Student class too.
    Person.call(this);
    this.school = this.decideSchool();
    console.log('Student instantiated. Your little monster is now going to ' + this.school);
}
// now we can make the Student inherit Person
Student.prototype = Person;
// keep in mind that just that line makes the Student.prototype.constructor be the Person's
// because we don't want that, we correct it with this line
Student.prototype.constructor = Student;
var student1 = new Student();