G's Function

by hslincoln

HTML

customer        

// I am the person constructor.
        function Person( name ){
 
            // I am the person's name.
            this.name = name;
 
            // I am the collection of traits.
            this.traits = {};
 
        }
 
 
        // Define a class method.
        Person.prototype.trait = function( name, value ){
 
            // Check to see if we are getting or setting the
            // given trait for this person.
            if (arguments.length == 2){
 
                // Set the trait.
                this.traits[ name ] = value;
 
                // Return this object.
                return( this );
 
            } else {
 
                // Return the given triat.
                return( this.traits[ name ] );
 
            }
 
        };
 
 
        // -------------------------------------------------- //
        // -------------------------------------------------- //
 
 
        // I am the girl contructor.
        function Girl( name, age, weight ){
 
            // Call the super constructor to initiate base class.
            Person.call( this, name );
 
            // Store the additional properties.
            this.trait( "age", (age - 5) );
            this.trait( "weight", (weight - 10) );
 
        }
 
        // Extend the Person class.
        Girl.prototype = new Person();
 
 
        // -------------------------------------------------- //
        // -------------------------------------------------- //
        // -------------------------------------------------- //
        // -------------------------------------------------- //
 
 
        // Create some girl instances.
        var sarah = new Girl( "Sarah", 32, 115 );
        var tricia = new Girl( "Tricia", 30, 125 );
 
        // Log the girls' traits.
        console.log( "Sarah:", sarah.traits );
        console.log( "Tricia:", tricia.traits );

JavaScript

var warehousejobs = ['bullet-dodger', 'pile-driver', 'hired gun'];
var testDate = new Date(1988, 11, 20);
var weapons = ['ice pick', 'piano wire', 'violin case'];

// 1. Contructor first
function Person(name, dob) {
 
    this.name = null;
    this.dob = null;
    
    this.setName(name);
    this.setDob(dob);
    
}

Person.prototype.setName = function (name) {
    // DOB checks
    if (typeof name !== 'string') {
       throw("Jonny Tightlips eh?");
    } else {
        this.name = name;
    }
};

Person.prototype.setDob = function (dob) {
    // DOB checks
    if (!(dob instanceof Date)) {
       throw("DOB is not a date");
    } else {
        this.dob = dob;
    }
};
              
Employee.prototype = new Person();
Employee.prototype.constructor = Employee;

function Employee (name, dob, jobtitle) {
  
    this.jobtitle = null;
    
    this.setName(name);
    this.setDob(dob);
    this.setJobtitle(jobtitle);
    
}

Employee.prototype.setJobtitle = function(jobtitle) {
    // jobtitle checks
    if (warehousejobs.indexOf(jobtitle)) {
        this.jobtitle = jobtitle;
    } else {
        throw("Is that a real jobtitle? Array says no");
    }
};

function Customer (name, dob, weapon) {
    this.weapon = null;
    
    this.setName(name);
    this.setDob(dob);
    this.setWeapon(weapon);
    
}

Customer.prototype.setWeapon = function (weapon) {
    // checks
    if (weapons.indexOf(weapon)) {
        this.weapon = weapon;
    } else {
        throw("Goddamn it, pick a fuckin shooter!");
    }       
};