開発環境
- OS X Lion - Apple(OS)
- Safari、Firefox + Firebug (Webプラウザ、プラグイン)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語:JavaScript
- JavaScript Library: jQuery
『初めてのJavaScript 第2版』(シェリー・パワーズ著(Shelley Powers著)、武舎 広幸+武舎 るみ訳、オライリー・ジャパン、2009年、ISBN978-4-87311-425-5) の13章(カスタムオブジェクトと例外の処理)練習問第13-2.を解いてみる。
その他参考書籍
2.
コード(BBEdit)
var Obj = function ( ) { // 隠蔽 var a = "JavaScript"; // こちらはアクセスできる this.b = "Python", // 隠蔽されたデータメンバーにアクセスするために用意 this.getA = function ( ) { return a; }, this.setA = function( o ) { a = o; }; }, o = new Obj(), result = [o.a, o.b, o.getA()].join("\n") + "\n"; o.setA("javascript"); o.b = "python"; result += [o.a, o.b, o.getA()].join("\n"); $('#pre0').text(result);
ちなみにPython3kの場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 # -*- coding: utf-8 -*- # pythonでは隠蔽はできない # 意図を示すたみに下線を利用する class C: def __init__(self): self._a = "JavaScript" self.b = "Python" def getA(self): return self._a def setA(self, a): self._a = a c = C() print(c.b, c.getA()) c.b = "python" c.setA("javascript") print(c.b, c.getA())
入出力結果(Terminal)
$ ./sample.py Python JavaScript python javascript $
0 コメント:
コメントを投稿