javascript - 外部 javascript 数组初始化

标签 javascript html

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

    }

我有一个 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:"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> 

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

最佳答案

你为什么不实际使用构造函数:

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

然后“addAccount”方法应该起作用。 否则,您只有一个具有某些属性(特性)的对象,但不属于客户端类。

关于javascript - 外部 javascript 数组初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21148528/

相关文章:

javascript - 我如何构建代码,例如 http ://fiddle. jshell.net/bwKZt/152/- HTML 和 Javascript

php - 从表中选择列1、列2,其中值=某个值

javascript - 如何使用javascript获取包含tr标签的数组中单击单元格的索引?

javascript - TypeScript 无法解析文件

javascript - D3.js ~ 使用 for 循环放置矩形并为其着色

javascript - 在不同屏幕分辨率下测试网页时如何调整网页大小以适应窗口?

php - 通过 Javascript 进行多图像选择的 HTML 表单

javascript - 多个段落,只需一个阅读更多按钮即可显示全部

javascript - 占位符不起作用

javascript - 在 insertBefore jQuery 之前删除 HTML