2017年4月19日水曜日

開発環境

メタプログラミングRuby 第2版(Paolo Perrotta (著)、角 征典 (翻訳)、オライリージャパン)の1部(メタプログラミング Ruby)、2章(月曜日: オブジェクトモデル)、2.5(クイズ: 絡み合ったモジュール)を JavaScript で取り組んでみる。

HTML5

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

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

JavaScript

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

let Printable = (spec) => {
    let that = spec || {},
        print = () => p('Printable: print'),
        prepareCover = () => p('Printable: prepare cover');

    that.print = print;
    that.prepareCover = prepareCover;

    return that;
};
let Document = (spec) => {
    let that = spec || {},
        printToScreen = () => {
            that.prepareCover();
            formatForScreen();
            that.print();
        },
        formatForScreen = () => p('Document: format for screen'),
        print = () => p('Document: print');

    that.printToScreen = printToScreen;
    that.print = print;
    
    return that;
};
let Book1 = (spec) => {
    let that = Document(spec);

    that = Printable(that);
    
    return that;
};
let Book2 = (spec) => {
    let that = Printable(spec);

    that = Document(that);

    return that;
};
let output = () => {
    let b1 = Book1(),
        b2 = Book2();

    p('1.');
    b1.printToScreen();
    p('2.');
    b2.printToScreen();
};

btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';

output();


  






 
						

0 コメント:

コメントを投稿

Comments on Google+: