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



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

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

当我调试时,我看到昵称和shift总是设置为好像没有为这些属性定义任何东西,即使它们是。到底发生了什么事?

详细信息:在IE9上运行,同样使用jquery2.1,在Win7机器上。

代码如下:

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

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

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

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

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

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

this.nickname = nickname || firstname;

或者,如果可以作为昵称传递,并且必须显式检查:

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

或者,如果可以同时为undefinednull:

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

如果你看一下jQuery等库,你会看到很多这样做。

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

您访问传递给this对象的构造函数的变量,而不首先将它们赋值给this

我将这样做:

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

demo here: http://jsbin.com/kojeguse/1/edit?js,console

最新更新