2017年4月22日土曜日

開発環境

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

EXERCISE(No. 3379)

コード(Emacs)

HTML5

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

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

JavaScript

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


let Car = (make, model, year, color, passengers, convertible, mileage) => {
    let that = {},
        started = false,
        start = () => started = true,
        stop = () => started = false,
        drive = () => {
            if (started) {
                p(`${make} ${model} goes zoom zoom!`);
            } else {
                p('You need to start the engine first.');
            }
        };

    that.start = start;
    that.stop = stop;
    that.drive = drive;

    return that;
};

let output = () => {
    let cadi = Car('GM', 'Cadillac'),
        chevy = Car('Chevy', 'Bel Air'),
        taxi = Car('Webville Motors', 'Taxi');

    [cadi, chevy, taxi].forEach((car) => {
        car.drive();
        car.start();
        car.drive();
        car.stop();
        car.drive();
    });
};
btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';

output();





    







						

0 コメント:

コメントを投稿