2017年5月28日日曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、Elisabeth Robson (著)、O'Reilly Media)の Chapter 13.(Extra strength objects - Using Prototypes)の CODE MAGNETS(No. 8370)を取り組んでみる。

CODE MAGNETS(No. 8370)

コード(Emacs)

HTML5

<pre id="output0"></pre>
<button id="run0">run</button>
<button id="clear0">clear</button>
<script src="sample2.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 Robot = (name, owned, year) => {
    let that = {},
        maker = 'ObjectsRUs',
        speak = () => 'speak',
        makeCoffee = () => 'makeCoffee',
        blinkLights = () => 'blinkLights';

    that.makeCoffee = makeCoffee;
    
    return that;
};

let Robby = (onOffSwitch) => {
    let that = Robot('Robby', 'Dr. Morbius', 1956),
        makeCoffee = () => 'Robby: makeCoffee';

    that.makeCoffee = makeCoffee;
    
    return that;
};

let Rosie = () => {
    let that = Robot('Rosie', 'George Jetson', 1962),
        cleanHouse = () => 'cleanHouse';

    that.cleanHouse = cleanHouse;
    
    return that;
};
let output = () => {
    let robby0 = Robby(true),
        rosie0 = Rosie();

    [robby0, rosie0].forEach((robot) => p(robot.makeCoffee()));
};

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

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

output();





    







						

0 コメント:

コメントを投稿