0

I'm creating an array of object type Employee, but one of the properties is not working as expected.

I'm using an array of objects of type Employee, that I define with a constructor statement block. Part of the constructor is that if no nickname property is defined, the first name is used as the nickname. Also, if no shift is defined, it is set as "1."

When I debug, I see that the nickname and shift are always set as if nothing was defined for those properties, even if they were. What gives?

The details: running on IE9, also using jquery2.1, on a Win7 machine.

Here's the code:

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

});
Aaron Thomas
  • 5,054
  • 8
  • 43
  • 89
  • 1
    not sure, but undefined is not a string... try remove that quotes ;) EDIT: you create an object with a "" value, then, you must test the empty string too. in your exemple, `typeof this.nickname === "string"` – Patrick Ferreira Jun 04 '14 at 15:57
  • 1
    @PatrickFerreira [`typeof`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) always returns a string. – Teemu Jun 04 '14 at 15:57
  • I should point out that in the code comments, I ref this SO question: http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property-in-javascript. It has more about the undefined property check. – Aaron Thomas Jun 04 '14 at 15:59
  • @AaronThomas Notice, that `typeof nickname` is never "undefined", unless you explicitely pass an undefined value as an argument, or pass only two arguments. – Teemu Jun 04 '14 at 16:04

5 Answers5

2

You check this.nickname before it is set so its type is still undefined.

if (typeof nickname === "undefined") {
        this.nickname = this.firstname;
    }
    else {
        this.nickname = nickname;
    }
Laoujin
  • 9,962
  • 7
  • 42
  • 69
1

You need to set them from inputs first.

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");
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
1

Or the really short version that all the cool kids use:

this.nickname = nickname || firstname;

or, if a null can be passed for nickname and must be checked for explicitly:

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

or, if it can be both undefined or null:

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

You'll see this done a lot if you look inside libraries like jQuery et al.

I like the first one, it's what I use. :)

Julian Jensen
  • 321
  • 2
  • 5
0

You accessing variables passed to constructor on this object, without assigning them to this first.

Pawel Furmaniak
  • 4,648
  • 3
  • 29
  • 33
0

This is how i'll do that :

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

Patrick Ferreira
  • 1,983
  • 1
  • 15
  • 31