2017年5月25日木曜日

Programming, JavaScript,

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、Elisabeth Robson (著)、O'Reilly Media)の Chapter 12.(Creating objects - Advanced Object Construction)の EXERCISE(No. 7861)を取り組んでみる。

EXERCISE(No. 7861)

コード(Emacs)

HTML5

<pre id="output0"></pre>
<button id="run0">run</button>
<button id="clear0">clear</button>
<script src="sample3.js"></script>

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    p = (x) => pre0.textContent += x + '\n',
    range = (start, end, step=1) => {
        let result = [];
        for (let i = start; i < end; i += 1) {
            result.push(i);
        }
        return result;
    };

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

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

    return that;
};

let output = () => {
    let chevy = Car('Chevy', 'Bel Air', 1957, 'red', 2, false, 1021, false),
        cadi = Car('GM', 'Cadillac', 1955, 'tan', 5, false, 12892, false);

    [chevy, cadi].forEach((car) => {
        car.drive();
        car.start();
        car.drive();
        car.stop();
        car.drive();
    });
};

let clear = () => pre0.textContent = '';

btn0.onclick = output;
btn1.onclick = clear;

output();





    







						

0 コメント:

コメントを投稿