プロトタイプベース・オブジェクト指向
at TECH
472:デフォルトの名無しさん
04/02/19 09:20
>>470
もう少しJSらしく書いてみたけど、普通にやると多態は駄目。
var BankAccount = new Object;
BankAccount.balance = 200;
document.write(BankAccount.balance + "<br>"); // 200
BankAccount.deposit = function(x) { return this.balance += x; }
document.write(BankAccount.deposit(50) + "<br>"); // 250
BankAccount.withdraw = function(x) {
return this.balance = Math.max(this.balance - x, 0);
}
document.write(BankAccount.withdraw(100) + "<br>"); // 150
document.write(BankAccount.withdraw(200) + "<br>"); // 0
function make_Account(){}
make_Account.prototype = BankAccount;
var myAccount = new make_Account;
// Mozilla 系 JS ならば var myAccont.__proto__ = BankAccount だけでもいい。
myAccount.balance = 100;
document.write(myAccount.balance + "<br>"); // 100
document.write(myAccount.deposit(50) + "<br>"); // 150
document.write(BankAccount.balance + "<br>"); // 0
次ページ続きを表示1を表示最新レス表示スレッドの検索類似スレ一覧話題のニュースおまかせリスト▼オプションを表示暇つぶし2ch
5405日前に更新/368 KB
担当:undef