简体   繁体   English

外部javascript数组初始化

[英]External javascript array initializing

I have an external client in a js file. 我在js文件中有一个外部客户端。

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);
    };

    }

and I have an html page. 而且我有一个html页面。 In it I have a script: 在其中我有一个脚本:

<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:"yeah@gmail.com", 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> 

Each client I initialize should have multiple accounts. 我初始化的每个客户端都应该有多个帐户。 Now I'm not sure how do I set the account array of the client. 现在,我不确定如何设置客户的帐户数组。 should it be a part of the constructor(inside the parentheses)? 它应该是构造函数的一部分(在括号内)吗? Because right now I can't use this array or get its data (I'm trying using another js file). 因为现在我无法使用此数组或获取其数据(我正在尝试使用另一个js文件)。

Why don't you actually make use of the constructor: 您为什么不真正使用构造函数:

myClients[0] = new client(11, "username", 1234, "regular", "John", "Doe", "Somewhere over the rainbow", "0523456789", "yeah@gmail.com");

Then the "addAccount" method should work. 然后,“ addAccount”方法应该起作用。 Otherwise you just have an object with some properties(attributes), but not of the class client. 否则,您只有一个具有某些属性(属性)的对象,而不是类客户端的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM