2017年4月19日水曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、Elisabeth Robson (著)、O'Reilly Media)の Chapter 5.(A trip to Objectville - Understanding Objects)、の SHARPEN YOUR PENCIL(No. 3150)を取り組んでみる。

SHARPEN YOUR PENCIL(No. 3150)

コード(Emacs)

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 getSecret = (file, secretPassword) => {
    file.opened += 1;
    if (secretPassword === file.password) {
        return file.contents;
    }
    return 'Invalid password! No secret for you.';
};

let setSecret = (file, secretPassword, secret) => {
    if (secretPassword === file.password) {
        file.opened = 0;
        file.contents = secret;        
    }
};

let superSecretFile = {
    level: 'classified',
    opened: 0,
    password: 2,
    contents: "Dr. Evel's next meeting is in Detroit.",
};    

let output = () => {
    let secret = getSecret(superSecretFile, 2);
    p(secret);
    
    setSecret(superSecretFile, 2, "Dr. Evel's next meeting is in Philadelphia.");
    secret = getSecret(superSecretFile, 2);
    p(secret);
};

btn0.onclick = output;

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

output();



    







						

0 コメント:

コメントを投稿