Prototypal Inheritance

by John Schulz

JavaScript

function Vertebrate() {
    this.spine = true;
}

function Mammal() {
    this.mammary_glands = true;
    this.hair = true;
}
Mammal.prototype = new Vertebrate();

function Human() {
    this.legs = 2;
}
Human.prototype = new Mammal();

function Male() {
    this.karyotype = "46,XY";
}
Male.prototype = new Human();

function Female() {
    this.karyotype = "46,XX";
}
Female.prototype = new Human();

function Person(Gender, first, last, dob) {
    var person = new Gender();

    person.first_name = first;
    person.last_name = last;
    person.date_of_birth = dob ? new Date(dob) : new Date();

    return person;
}

function Programmer(person, languages) {
    var programmer = person;

    programmer.languages = languages;
    programmer.can_code = !! (Math.round(Math.random()));
    programmer.sleeps = false;

    return programmer;
}

var john = new Person(Male, 'John', 'Schulz', '07/26/1976');
new Programmer(john, ['JavaScript', 'Perl', 'Bash']);

console.log('john', john, 'sleep?', john.sleeps, 'can code?', john.can_code);