プロトタイプベース・オブジェクト指向 at TECH
[2ch|▼Menu]
463:デフォルトの名無しさん
04/02/17 16:56
>>462
勉強を兼ねて恒例の bank account を書いてみました。
委譲/継承も多態もできているみたいです。
個人的には self をメソッドで暗示的引数に指定しなくていいぶん、
Python よりはスジがよいように思います。

<script type="text/javascript">
var bank_account = new Object;

function ba_set_balance(_x) {this.balance = _x; return this}
bank_account.set_balance = ba_set_balance;
function ba_get_balance() {return this.balance}
bank_account.get_balance = ba_get_balance;

document.write( bank_account.set_balance(200).get_balance() + "<br>"); // => 200

function ba_deposit(_x) {
 this.set_balance(this.get_balance() + _x);
 return this}
bank_account.deposit = ba_deposit;

document.write( bank_account.deposit(50).get_balance() + "<br>"); // => 250

function ba_withdraw(_x) {
 this.set_balance(Math.max(this.get_balance() - _x, 0));
 return this}
bank_account.withdraw = ba_withdraw;

document.write( bank_account.withdraw(100).get_balance() + "<br>"); // => 150
document.write( bank_account.withdraw(200).get_balance() + "<br>"); // => 0


次ページ
続きを表示
1を表示
最新レス表示
スレッドの検索
類似スレ一覧
話題のニュース
おまかせリスト
▼オプションを表示
暇つぶし2ch

5405日前に更新/368 KB
担当:undef