2017年4月21日金曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、Elisabeth Robson (著)、O'Reilly Media)の Chapter 5.(A trip to Objectville - Understanding Objects)、の SHARPEN YOUR PENCIL(No. 3311)を取り組んでみる。

SHARPEN YOUR PENCIL(No. 3311)

コード(Emacs)

HTML5

<pre id="output0"></pre>
<button id="run0">run</button>
<button id="clear0">clear</button>

<script src="sample6.js"></script>

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    p = (x) => pre0.textContent += x + '\n';

let makeEightBall = () => {
    let that = {},
        index = 0,
        advice = ['yes', 'no', 'maybe', 'not a chance'],
        shake = () => {
            index += 1;
            if (index >= advice.length) {
                index = 0;
            }
        },
        look = () => advice[index];

    that.shake = shake;
    that.look = look;
            
    return that;
};

let eightBall = makeEightBall();

let output = () => {    
    eightBall.shake();
    p(eightBall.look());
};

btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';

output();





    







						

0 コメント:

コメントを投稿