2017年4月17日月曜日

開発環境

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

OBJECT MAGNETS(No. 2951)

コード(Emacs)

HTML5

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

<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 dog = {
    name: 'fido',
    weight: 20.2,
    age: 4,
    breed: 'mixed',
    activity: 'fetch balls'
};

let output = () => {
    let bark;

    if (dog.weight > 20) {
        bark = 'WOOF WOOF';
    } else {
        bark = 'woof woof';
    }
    let speak = `${dog.name} says ${bark} when he wants to ${dog.activity}`;
    
    p(speak);
};

btn0.onclick = output;

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

output();



    




 
						

0 コメント:

コメントを投稿

Comments on Google+: