Pages

Sunday, September 25, 2016

Let's go to the mall! (JavaScript)

function castActors(parts) {

    /* Molds for the various parts */
    /* bottom-up character development */
    
    // Earthling mold (an abstract base class)
    function Earthling(name, country, favoriteBook) {
        this.name = name;
        this.country = country;
        this.planet = "Earth";
        this.favoriteBook = favoriteBook;
    }
    Earthling.prototype = {
        say: function(anything) {
            var output = this.name;
            output += " (" + this.placeOfOrigin() + "): ";
            output += anything;
            alert(output);
            console.log(output);
        },
        sayHello: function() {
            throw new Error("Not implemented!");
        },
        sayLetsGo: function() {
            throw new Error("Not implemented!");
        },
        placeOfOrigin: function() {
            return this.country;
        },
        constructor: Earthling,
    };

    // Mexican mold
    function Mexican(name, favoriteBook) {
        Earthling.call(this, name, "Mexico", favoriteBook);
    }
    Mexican.prototype = Object.create(Earthling.prototype);
    Mexican.prototype.constructor = Mexican;
    Mexican.prototype.sayHello = function() {
        this.say("Hola!");
    }
    Mexican.prototype.sayLetsGo = function() {
        this.say("Vamonos!");
    }

    // Chinese mold
    function Chinese(name, favoriteBook) {
        Earthling.call(this, name, "China", favoriteBook);
    }
    Chinese.prototype = Object.create(Earthling.prototype);
    Chinese.prototype.constructor = Chinese;
    Chinese.prototype.sayHello = function() {
        this.say("Chi fan le ma?");
    }
    Chinese.prototype.sayLetsGo = function() {
        this.say("Qu ba!");
    }

    // American mold
    function American(name, state, favoriteBook) {
        Earthling.call(this, name, "USA", favoriteBook);
        this.state = state;
    }
    American.prototype = Object.create(Earthling.prototype);
    American.prototype.constructor = American;
    American.prototype.sayHello = function() {
        this.say("Hay");
    };
    American.prototype.sayLetsGo = function() {
        this.say("Let's go!");
    };
    American.prototype.placeOfOrigin = function() {
        var whereIHailFrom = "";
        if (this.state != undefined) {
            whereIHailFrom += this.state + ", "; 
        }
        whereIHailFrom += this.country;
        return whereIHailFrom;
    };

    // American Girl mold
    function AmericanGirl(name, state, favoriteBook) {
        American.call(this, name, state, favoriteBook);
    }
    AmericanGirl.prototype = Object.create(American.prototype);
    AmericanGirl.prototype.constructor = AmericanGirl;
    AmericanGirl.prototype.sayHello = function() {
        this.say("What's up?");
    }

    // Japanese American Girl mold
    function JapaneseGirl(name, favoriteBook) {
        Earthling.call(this, name, "Japan", favoriteBook);
    }
    JapaneseGirl.prototype = Object.create(Earthling.prototype);
    JapaneseGirl.prototype.constructor = JapaneseGirl;
    JapaneseGirl.prototype.sayHello = function() {
        this.say("Hello kiddies!");
    }

    // Valley Girl mold
    function ValleyGirl(name, favoriteBook) {
        AmericanGirl.call(this, name, "California", favoriteBook);
    }
    ValleyGirl.prototype = Object.create(AmericanGirl.prototype);
    ValleyGirl.prototype.constructor = ValleyGirl;
    ValleyGirl.prototype.sayLetsGo = function() {
        this.say("OMG let's like go to the mall dudes!");
    }
    
    /* The solidified parts */
    
    parts.sun = new Chinese("Sun Zhiyue", "Dream of the Red Chamber");
    parts.lara = new American("Lara Bethany", "Oregon", "Doctor Zhivago");
    parts.sam = new AmericanGirl("Sam Parkinglot", "New York", "The Wizard of Oz");
    parts.julie = new ValleyGirl("Julie Richwoman", "Kingdom Come");
    parts.dora = new Mexican("Dora the Mall Explorer", "Good Score, Dora!")
    parts.kit = new JapaneseGirl("Kiddie White", "Merry Christmas, Kiddie!");    
}

function rollOpeningCredits(title) {
    var openingCredits = title;
    openingCredits += "\nIntroducing Julie Richwoman as the Valley Girl";
    alert(openingCredits);
    console.log(openingCredits);    
}

// top-down script
function performScene(actors) {
    actors.kit.sayHello();
    actors.sam.sayHello();
    actors.lara.sayHello();
    actors.dora.sayHello();
    actors.sun.sayHello();
    actors.julie.sayLetsGo();
    actors.sam.sayLetsGo();
    actors.dora.sayLetsGo();
    actors.sun.sayLetsGo();    
}

function rollClosingCredits(cast) {
    alert("CAST: " + Object.keys(cast));
    console.log("* * * CAST * * *");
    console.log(cast); // meet the actors backstage!
}

function produceScene() {
    /* development: set the scene */
    var title = "Let's go to the mall!";
    var roles = {
        kit: undefined, // a Japanese Girl
        sam: undefined, // an All-American Girl
        lara: undefined, // an Oregonian
        dora: undefined, // the Mall Explorer
        sun: undefined, // a Chinese Tiger Mother
        julie: undefined, // the Valley Girl
    }
    
    /* pre-production: actual actors are cast */
    castActors(roles);
    
    /* production: the performance */
    rollOpeningCredits(title)
    performScene(roles); // action!    
    rollClosingCredits(roles);    
}

produceScene();