如何通过函数传递数组?

时间:2017-10-21 19:39:56

标签: javascript arrays function parameters

我正在处理一些代码,用户可以创建所有具有名字,姓氏,电话号码等的客户端...我想添加一个按钮,用户可以在其中输入关于客户端的多个注释&# 39;家人但不知道如果将它们添加到客户端,如果这是有道理的。这是我的代码片段:

function client(firstName, lastName, phoneNumber, address, hireDate, frequency, cost, entry) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.phoneNumber = phoneNumber;
    this.address = address;
    this.hireDate = hireDate;
    this.frequency = frequency;
    this.cost = cost;
    this.entry = entry;
}
var laura = new client("laura", "simmerman", "1111111111", "laura rd", "01/01/01", "weekly", "100", "garage-1111");
var clients = [laura];

所以基本上要获得第一个客户端的名字我将使用:clients [0] .firstName;

我想要做的是添加一个名为familyNotes的数组作为另一个参数,如下所示:

function client(firstName, lastName, phoneNumber, address, hireDate, frequency, cost, entry, familyNotes[]) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.phoneNumber = phoneNumber;
    this.address = address;
    this.hireDate = hireDate;
    this.frequency = frequency;
    this.cost = cost;
    this.entry = entry;
    this.familyNotes = familyNotes[];
}

然后得到我要调用的第一个音符:clients [0] .familyNotes [0];

然而,这似乎不起作用,我是否错误地调用了数组?在这种情况下,有人能告诉我一种将数组作为参数的方法吗?

3 个答案:

答案 0 :(得分:0)

传递参数

时删除括号
public function actionUpload(){

    $imgName=md5(uniqid()).'.jpg';
    $this->base64_to_jpeg($base64_string, $imgName);
}

答案 1 :(得分:0)

function client(firstName, lastName, phoneNumber, address, hireDate, frequency, cost, entry, familyNotes) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.phoneNumber = phoneNumber;
    this.address = address;
    this.hireDate = hireDate;
    this.frequency = frequency;
    this.cost = cost;
    this.entry = entry;
    this.familyNotes = familyNotes;
}

答案 2 :(得分:0)

正如user2887596所说,你需要删除函数声明function client(firstName, ... , familyNotes) {中的方括号,但是当你将属性赋值给this

应为this.familyNotes = familyNotes;