2017年5月6日土曜日

開発環境

メタプログラミングRuby 第2版(Paolo Perrotta (著)、角 征典 (翻訳)、オライリージャパン)の1部(メタプログラミング Ruby)、6章(金曜日: コードを記述するコード)、6.7(フックメソッド)、6.7.1(その他のフック)を JavaScript で取り組んでみる。

HTML5

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

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

JavaScript

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

let M1 = (othermod) => {
    p(`M1 は ${othermod} にインクルードされた`);

    let that = {};

    return that;
};
let M2 = (othermod) => {
    p(`M2 は ${othermod} にプリペンドされた`);

    let that = {};

    return that;
};

let C = (() => {
    let that1 = M1('C'),
        that2 = M2('C');
    
    return () => {
        let that = {};

        Object.keys(that1).forEach((k) => that[k] = that1[k]);
        Object.keys(that2).forEach((k) => that[k] = that2[k]);

        return that;
    };
})();

let M = (() => {
    let that = {},
        addMethod = (name, method) => {
            p(`新しいメソッド:M#${method}`);
            that[name] = method;
        };

    addMethod('myMethod', () => 'myMethod');
    
    return () => {
        
        return that;    
    };
})();

let output = () => {
    p('6.7 フックメソッド');
    p('6.7.1 その他のフック');
    let m = M();
    p(m.myMethod());
};

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

output();




  








						

0 コメント:

コメントを投稿