Call & Apply demo

by Julien Etienne

HTML

<h1>Bla bla bla</h1>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

CSS

body{
    background: #222;
    color: white;
}
div {
    position: relative;
    float: left;
    margin: 0 0.3em;
}

JavaScript

var Jane = {
    wd1: ' you know ',
    wd2: ' and to think ',
    wd3: ' can you believe it? ',
    alot: 6
};

var Mike = {
    wd1: ' and then ',
    wd2: ' just like that ',
    wd3: ' i was lost for words ',
    tooMuch: 12
};

var talkTooMuch = function (howMuch) {
    var i;
    for (i = 0; i < howMuch; i++) {
        var raNum = Math.random();
        var rand = raNum < 0.333 ? this.wd1 : raNum > 0.667 ? this.wd2 : this.wd3;
        document.getElementsByTagName('div')[i].innerHTML = rand;
    }
};
// Change this to (Mike, Mike.tooMuch);
   talkTooMuch.call(Jane, Jane.alot);

//talkTooMuch.apply(Mike, [Mike.tooMuch]);