Javascript对象构造函数数组未按预期工作

时间:2014-06-04 15:53:51

标签: javascript

我正在创建一个对象类型为Employee的数组,但其中一个属性未按预期工作。

我使用的是Employee类型的对象数组,我用构造函数语句块定义。构造函数的一部分是,如果没有定义nickname属性,则将第一个名称用作昵称。此外,如果未定义班次,则将其设置为" 1。"

当我调试时,我看到昵称和移位总是设置,好像没有为这些属性定义任何内容,即使它们是。是什么给了什么?

细节:在Win9机器上运行IE9,也使用jquery2.1。

以下是代码:

// make sure doc ready first
$(document).ready(function(){
    function Employee(firstname, lastname, nickname, employeeID, shift, serviceTruck, pickupTruck, serviceTruckWO){
        this.firstname = firstname;
        this.lastname = lastname;
        /*
         * Check if the nickname is present. If not,
         * set as lastname. Credit to SO for
         * how to do this properly, ref
         * http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property-in-javascript
         */
        if (typeof this.nickname === "undefined") {
            this.nickname = this.firstname;
        }
        else {
            this.nickname = nickname;
        }
        this.employeeID = employeeID;
        if (typeof this.shift === "undefined") {
            this.shift = "1";
        }
        else {
            this.shift = shift;
        }
        this.serviceTruck = serviceTruck;
        this.pickupTruck = pickupTruck;
        this.serviceTruckWO = serviceTruckWO;
    }
    var Employees = new Array();
    Employees[0] = new Employee("John", "Smith", "Smithy", "1234", "2", "ST1", "PU1", "WO1");
    Employees[1] = new Employee("Jane", "Doe", "", "1235", "", "ST2", "PU2", "WO2");

});

5 个答案:

答案 0 :(得分:2)

在设置之前检查this.nickname,因此其类型仍未定义。

if (typeof nickname === "undefined") {
        this.nickname = this.firstname;
    }
    else {
        this.nickname = nickname;
    }

答案 1 :(得分:1)

您需要先从输入设置它们。

function Employee(firstname, lastname, nickname, employeeID, shift, serviceTruck, pickupTruck, serviceTruckWO){
    this.firstname = firstname;
    this.lastname = lastname;
    /*
     * Check if the nickname is present. If not,
     * set as lastname. Credit to SO for
     * how to do this properly, ref
     * http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property-in-javascript
     */
      this.nickname = nickname;
    if (typeof this.nickname === "undefined") {
        this.nickname = this.firstname;
    }
    else {
        this.nickname = nickname;
    }
    this.employeeID = employeeID;
     this.shift = shift ;
    if (typeof this.shift === "undefined") {
        this.shift = "1";
    }
    else {
        this.shift = shift;
    }
    this.serviceTruck = serviceTruck;
    this.pickupTruck = pickupTruck;
    this.serviceTruckWO = serviceTruckWO;
}
var Employees = new Array();
Employees[0] = new Employee("John", "Smith", "Smithy", "1234", "2", "ST1", "PU1", "WO1");
Employees[1] = new Employee("Jane", "Doe", "", "1235", "", "ST2", "PU2", "WO2");

答案 2 :(得分:1)

或者所有酷孩子使用的真正简短的版本:

this.nickname = nickname || firstname;

或者,如果可以为昵称传递 null ,则必须明确检查:

this.nickname = nickname === null ? firstname : nickname;

或者,如果它可以是未定义 null

this.nickname = nickname && nickname !== null ? nickname : firstname;

如果你看一下jQuery等人的文库,你会发现这很多。

我喜欢第一个,这就是我使用的。 :)

答案 3 :(得分:0)

您访问传递给this对象上的构造函数的变量,而不是先将它们分配给this

答案 4 :(得分:0)

我就是这样做的:

function Employee(firstname, lastname, nickname, employeeID, shift, serviceTruck, pickupTruck, serviceTruckWO){
    this.firstname = firstname;
    this.lastname = lastname;
    this.employeeID = employeeID;
    this.serviceTruck = serviceTruck;
    this.pickupTruck = pickupTruck;
    this.serviceTruckWO = serviceTruckWO;

    this.nickname = nickname; // if all right, already set
    this.shift = shift;       // if all right, already set

    // special cases, else not needed cause you already define the value ;)
    if (nickname === "" || nickname === null || nickname === undefined)
        this.nickname = this.firstname;

    if (shift === "" || shift === null || shift === undefined)
        this.shift = "1";
}

演示:http://jsbin.com/kojeguse/1/edit?js,console