2017-11-11 39 views
0

我正在尋找一些關於爲下面的代碼創建輸出的最簡單方法的指導。只需簡單的用戶輸出到HTML文檔。任何指導都會有所幫助。還有關於如何使用連接方法的任何指導,以便我可以鏈接兩個屬性,例如Lname和accNo?Javascript創建方法 - 如何輸出

這個想法將有用戶輸入框(用於存款/取款),然後使用按鈕/按鈕來完成功能並顯示結果。

我必須指出的是,你的方法不會制定出的是
<script> 

function Account(fname, lname, accNo, amount) { 
this.fname = fname; 
this.lname = lname; 
this.accNo = accNo; 
this.balance = amount; 
this.bankDeposit = deposit; 
this.bankWithdraw = withdraw; 

} 

function deposit(amount) { 
this.balance += amount; 

} 

function withdraw(amount) { 
if (amount <= this.balance) {  
this.balance -= amount;  
} 
if (amount > this.balance) { 
alert("Declined");   
} 
} 

function joinName() { 


} 


var P1 = new Account("Nathan", "Smith", "SA001", 500); 
var P2 = new Account("John", "Smith", "SA002", 1500); 



</script> 


</body> 
</html> 
+0

此代碼目前正常工作嗎?.....不認爲它會工作 – raykrow

+0

是的我在代碼上做了一些更多的工作來顯示結果,它的工作 – magpienath

回答

1

第一。他們需要在您的Account類原型上聲明。

Account.prototype.deposit = function(amt) { 
    ... 
} 

對於要輸出HTML的問題絕對最簡單的方法是

Account.prototype.print = function() { 
    document.body.innerHTML = JSON.stringify(this); 
} 

對於結合性質的問題,你會怎麼做

Account.prototype.joinName = function() { 
    return this.fname + ‘ ‘ + this.lname; 
} 

但是,如果您打算使用這個加入的名字很多,我只是將它設置在構造函數中。

this.joinedName = fname + ‘ ‘ + lname