2014-01-15 45 views
1

我在js文件中有一个外部客户端。外部JavaScript数组初始化

function client(Id,userName,code,type, firstName, SurName, address, phoneNum, Email) 
{ 
    this.Id = Id; 
    this.userName = userName; 
    this.code = code; 
    this.firstName=firstName; 
    this.SurName=SurName; 
    this.ddress=address;   
    this.phoneNum=phoneNum; 
    this.Email =Email; 
    this.clientAccounts = []; 

    this.addAccount = function(account) 
    { 
     this.clientAccounts.push(account); 
    }; 

    } 

我有一个html页面。在这里面我有一个脚本:

<script type="text/javascript"> 
var myClients =new Array(); 
myClients[0]= {firstName:"John", SurName: "Doe" ,Id:11,Password:1234, Address:"Some where over the rainbow", Pnumber:0523456789, Email:"[email protected]", Type: "regular"}; 
var account = new account(232, "young"); 
var deposit = new depositAccount(232, "young", 1000, 2555); 
myClients[0].addAccount(deposit); 

//clientAccounts.push(myClients[0]); 
</script> 

每个客户我初始化应该有多个帐户。现在我不确定如何设置客户端的帐户数组。它应该是构造函数的一部分(在括号内)? 因为现在我不能使用这个数组或获取它的数据(我试图使用另一个js文件)。

+2

希望我明白你在问什么。 –

+0

这应该是像oop。如何使阵列成为客户端的属性? – user2674835

+0

myClients [0] .Accounts = [];你在找什么? –

回答

4

你为什么不真正使用的构造方法:

myClients[0] = new client(11, "username", 1234, "regular", "John", "Doe", "Somewhere over the rainbow", "0523456789", "[email protected]"); 

那么“addAccount”的方法应该工作。 否则,你只是有一些属性(属性)的对象,但不是类客户端。

+0

谢谢,我以为我做得对... – user2674835