2013年3月11日月曜日

開発環境

『初めてのJavaScript 第2版』(シェリー・パワーズ著(Shelley Powers著)、武舎 広幸+武舎 るみ訳、オライリー・ジャパン、2009年、ISBN978-4-87311-425-5) の13章(カスタムオブジェクトと例外の処理)練習問第13-5.を解いてみる。

その他参考書籍

5.

コード(BBEdit)

JavaScript

var Obj = function (  ) {
    var background = "#fff",
        state = "on";
    this.changeState = function (  ) {
        if ( state === "on" ) {
            state = "off";
            background = "#000";
        } else {
            state = "on";
            background = "#fff";
        }
    },
    this.getColor = function (  ) {
        return background;
    },
    this.getState = function (  ) {
        return state;
    };
},
    o = new Obj(),
    f = function (  ) {
        o.changeState();
        $('#d0').css('background-color', o.getColor());
    };

HTML

<input type="button" value="changeState" onclick="f()"/>
<input type="button" value="getColor" onclick="alert(o.getColor())" />
<input type="button" value="getState" onclick="alert(o.getState())" />

0 コメント:

コメントを投稿