[表示 : 全て 最新50 1-99 101- 201- 301- 401- 501- 601- 701- 801- 901- 1001- 2chのread.cgiへ]
Update time : 05/09 23:17 / Filesize : 368 KB / Number-of Response : 1002
[このスレッドの書き込みを削除する]
[+板 最近立ったスレ&熱いスレ一覧 : +板 最近立ったスレ/記者別一覧] [類似スレッド一覧]


↑キャッシュ検索、類似スレ動作を修正しました、ご迷惑をお掛けしました

プロトタイプベース・オブジェクト指向



463 名前:デフォルトの名無しさん mailto:sage [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






[ 続きを読む ] / [ 携帯版 ]

全部読む 前100 次100 最新50 [ このスレをブックマーク! 携帯に送る ] 2chのread.cgiへ
[+板 最近立ったスレ&熱いスレ一覧 : +板 最近立ったスレ/記者別一覧](;´∀`)<368KB

read.cgi ver5.27 [feat.BBS2 +1.6] / e.0.2 (02/09/03) / eucaly.net products.
担当:undef