2017年6月2日金曜日

Programming, JavaScript,

開発環境

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

EXERCISE(No. 8678)

コード(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',
    range = (start, end, step=1) => {
        let result = [];
        for (let i = start; i < end; i += 1) {
            result.push(i);
        }
        return result;
    };

let Robot = (name, year, owned, game) => {
    let that = {},
        maker = 'ObjectsRUs',
        makeCoffee = () => p('makeCoffee'),
        blinkLights = () => p('blinkLights'),
        toString = () =>
        `${name} was made by ${maker} in ${year} and is owned by ${owned}`;

    that.makeCoffee = makeCoffee;
    that.blinkLights = blinkLights;
    that.toString = toString;
    
    return that;
};

let SpaceRobot = (name, year, owned, homePlanet) => {
    let that = Robot(name, year, owned),
        speak = () => p(`${name} says Sir, If I may venture  opinion…`),
        pilot = () => p(`${name} says Thrusters? Are they important?`);

    that.speak = speak;
    that.pilot = pilot;
    
    return that;
};

let output = () => {
    let c3po = SpaceRobot('C#PO', 1977, 'Luke Skywalker', 'Tatooine');

    c3po.speak();
    c3po.pilot();
    p(c3po);

    let simon = SpaceRobot('Simon', 2009, 'Carla Diana', 'Earth');
    
    simon.makeCoffee();
    simon.blinkLights();
    simon.speak();
};

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

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

output();





    




 
						

0 コメント:

コメントを投稿

Comments on Google+: