2017年4月12日水曜日

開発環境

Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 10.(Function Decorators: Wrapping Functions) の SHARPEN YOUR PENCIL(No. 7593) を JavaScript で取り組んでみる。

SHARPEN YOUR PENCIL(No. 7593)

コード(Emacs)

HTML5

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

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

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    session = {};

let checkLoggedIn = (fn) => {
    return (...args) => {
        if (session['logged_in']) {
            return fn(...args);
        }
        return 'You are NOT logged in';
    };
};

let test = checkLoggedIn(
    (a, b) => {
        return `logged in(${a + b})`;
    }
);

let output = () => {
    pre0.textContent += test() + '\n';
    session['logged_in'] = true;
    pre0.textContent += test(5, 10) + '\n';
    delete session['logged_in'];
    pre0.textContent += test() + '\n';
};

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

output();












						

0 コメント:

コメントを投稿

Comments on Google+: