Search...

Saturday, February 11, 2012

Create function, objects in javascript


<html>
  <head>
    <style>
    </style>
    <script src="jquery-1.3.2.min.js" type="text/javascript"></script>
  </head>
  <body>
Bnalance<br>
  <input type="text" id="amt"><br>
<input value="Deposit" type="button" onclick="f_dep();">
<input value="Withdrawl" type="button" onclick="f_with()"><br>
<lable id="result"></lable><br>
 </body>
</html>

<script type="text/javascript">
  Account = function()
  {
    this.balance=0;
  }
 
  Account.constructor=function(bal)
  {
    this.balance=bal;
  }

  Account.prototype.Deposit=function(amt)
  {
  if(parseInt(amt)>0){
    this.balance+=parseInt(amt);
  }
  }

  Account.prototype.withdrawl=function(amt)
  {
   if(this.balance!=0){
    this.balance-=parseInt(amt);
   }
  }

  Account.prototype.getBalance=function()
  {
    return this.balance;
  }

  var acc=new Account();

  function f_dep()
  {
var t1= document.getElementById("amt");
result.innerHTML="<br>Before Deposit "+acc.getBalance();
acc.Deposit(t1.value);
result.innerHTML+="<br>After deposit "+acc.getBalance();

  }

  function f_with()
  {
var t1= document.getElementById("amt");
result.innerHTML="<br>Before Withdrawl "+acc.getBalance();
acc.withdrawl(t1.value);
result.innerHTML+="<br>After Withdrawl "+acc.getBalance();

  }
</script>

No comments: