Pages

Thursday, September 22, 2016

A Close Encounter of the JavaScript Kind

// DEFINE A CONCEPTUAL PERSON
function Person(name, language) {
    this.myName = name;    
    this.myLanguage = language;
}
Person.prototype = {
    myName: undefined,
    myLanguage: undefined,
    helloInMyLanguage: function() {
        throw new Error("Not implemented!");
    },
    helloInEnglish: function() {
        return "Hello";
    },
    speaks: function(language) {
        if (language == "English") {
            return true;
        }
        else if (language == this.myLanguage) {
            return true;
        }
        else {
            return false;
        }
    },        
    sayHello: function(otherPerson) {
        var hello;
        if (otherPerson.speaks(this.myLanguage)) {
            hello = this.helloInMyLanguage();
        }
        else {
            hello = this.helloInEnglish();
        }
        alert(this.myName + ": " + hello);
    },
}

// DEFINE A CONCEPTUAL PERSON FROM CHINA
function PersonFromChina(name) {
    Person.call(this, name, "Chinese"); // call super-constructor
    this.helloInMyLanguage = function() {
        return "Ni Hao";
    };    
    this.helloInEnglish = function() {
        return "Hi";
    };
}
PersonFromChina.prototype = Object.create(Person.prototype);
PersonFromChina.prototype.constructor = PersonFromChina;

// DEFINE A CONCEPTUAL PERSON FROM THE UNITED STATES
function PersonFromUnitedStates(name) {
    Person.call(this, name, "English"); // call super-constructor
    this.helloInMyLanguage = function() {
        return this.helloInEnglish();
    };
} 
PersonFromUnitedStates.prototype = Object.create(Person.prototype);
PersonFromUnitedStates.prototype.constructor = PersonFromUnitedStates;

// DEFINE A CONCEPTUAL AMERICAN COWBOY
function AmericanCowboy(name) {
    PersonFromUnitedStates.call(this, name); // call super-constructor
    this.helloInEnglish = function() {
        return "Howdy!";
    }
}
AmericanCowboy.prototype = Object.create(PersonFromUnitedStates.prototype);
AmericanCowboy.prototype.constructor = AmericanCowboy;

// DEFINE A CONCEPTUAL AMERICAN STUDENT
function AmericanStudent(name) {
    PersonFromUnitedStates.call(this, name); // call super-constructor
    this.helloInEnglish = function() {
        return "What's up?";
    };
}
AmericanStudent.prototype = Object.create(PersonFromUnitedStates.prototype);
AmericanStudent.prototype.constructor = AmericanStudent;

// CREATE A COLLECTION OF ACTUAL PEOPLE
var People = {
    cowboy: new AmericanCowboy("John Wayne"),
    student1: new AmericanStudent("David Spurgeon"),
    student2: new AmericanStudent("Lara Spurgeon"),
    student3: new PersonFromChina("Wang Xuesheng"),
    student4: new PersonFromChina("Sun Xuesheng"),
}

// DEFINE A CONCEPTUAL ENCOUNTER
var Encounter = {    
    begin: function(person1, person2) {
        person1.sayHello(person2);
        person2.sayHello(person1);
    },
    close: function() {
        console.log("The End");
    }
}

/* MAKE AN ENCOUNTER HAPPEN! */

// BEGIN AN ACTUAL ENCOUNTER...
Encounter.begin(People.cowboy, People.student3);

// ...THEN CLOSE THE ENCOUNTER.
Encounter.close();