2017年5月26日金曜日

開発環境

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

EXERCISE(No. 7993)

コード(Emacs)

HTML5

<pre id="output0"></pre>
<button id="run0">run</button>
<button id="clear0">clear</button>
<script src="sample4.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 dogCatcher = (obj) => obj.type === Dog;

let Cat = (name, breed, weight) => {
    let that = {},
        type = Cat;

    that.type = type;
    
    return that;
};
let meow = Cat('Meow', 'siamese', 10),
    whiskers = Cat('Whiskers', 'Mixed', 12);

let fido = {name: 'Fido', breed: 'Mixed', weight: 38};

let Dog = (name, breed, weight) => {
    let that = {},
        bark = () => {
            if (weight > 25) {
                p(`${name} says Woof!`);
            } else {
                p(`${name} says Yip!`);
            }
        },
        type = Dog;

    that.name = name;
    that.bark = bark;
    that.type = type;

    return that;
};

let fluffy = Dog('Fluffy', 'Poodle', 30),
    spot = Dog('Spot', 'Chihuahua', 10);

    dogs = [meow, whiskers, fido, fluffy, spot];

let output = () => {

    dogs.forEach((dog) => {
        if (dogCatcher(dog)) {
            p(`${dog.name} is a dog`);
        }
    });
};

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

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

output();





    







						

0 コメント:

コメントを投稿