2017年4月14日金曜日

開発環境

メタプログラミングRuby 第2版(Paolo Perrotta (著)、角 征典 (翻訳)、オライリージャパン)の1部(メタプログラミング Ruby)、2章(月曜日: オブジェクトモデル)、2.2(オブジェクトモデルの内部)、2.2.5(ネームスペースを使う)を JavaScript で取り組んでみる。

HTML5

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

<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';
    };

let Text = () => {
    let that = {},
        method = () => 'Hello, Text.method!';

    that.method = method;
    
    return that;
};
let Bookworm = () => {
    let that = {},
        Text = () => {
            let that = {},
                method = () => 'Hello, Bookworm.Text.method!';

            that.method = method;
            
            return that;            
        },
        method = () => 'Hello, Bookworm.method!';

    that.Text = Text;
    that.method = method;
    
    return that;
};
let output = () => {
    let t = Text(),
        b = Bookworm(),
        bt = b.Text();

    p(t.method());
    p(b.method());
    p(bt.method());
};


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

output();


  









						

0 コメント:

コメントを投稿