构造函数

function C(amount){
this.balance=amount;
this.deposit=deposit;
this.withdraw=withdraw;
this.toString=toString;
}
function deposit(amount){
this.balance+=amount;
}
function withdraw(amount){
if(amount<=this.balance){
this.balance-=amount;
}
if(amount>this.balance){
alert("余额不足");
}
}
function toString(amount){
return "余额:"+this.balance;
}
var account=new C(500);
account.deposit(1000);
console.log(account.toString());

account.withdraw(750);
console.log(account.toString());

account.withdraw(375);
console.log(account.toString());

posted @ 2016-07-31 14:49  ArielChen  阅读(83)  评论(0编辑  收藏  举报