-4

I'm studying Javascript and I found a problem. I must define the constructor given some parameters. I insert them but I wanted to insert an if to check when the user inserts nothing. I'm working with the parasitic Inheritance model, but I don't know how to insert this case. Here is the part of my constructor:

function user(_id, userName, email, password, firstName, lastName, date_created, playlists) {

if (firstName === 'undefined') {
    firstName = userName;

}
if (lastName === 'undefined') {
    lastName = userName;
}
return {
    _id: _id,
    userName: userName,
    email: email,
    password: password,
    firstName: firstName,
    lastName: lastName,
    date_created: new Date(),
    playlists: []

};
}

Suppose that the user does not use firstName and LastName. It will return undefined. But I want to store a default value. How do I do that?

Dan O
  • 6,022
  • 2
  • 32
  • 50
pp94
  • 133
  • 5
  • 19
  • You can put the `if` logic between the declaration of the function and the `return` statement. In fact, you should not put your logic anyplace else (after the `return` it would not get executed) – SJuan76 Sep 30 '14 at 18:48
  • possible duplicate of [Set a default parameter value for a JavaScript function](http://stackoverflow.com/questions/894860/set-a-default-parameter-value-for-a-javascript-function) – Eric Alberson Sep 30 '14 at 18:48
  • I would think you could use something like: `firstName: (firstName != null) ? firstName : 'default value'` – Joel Etherton Sep 30 '14 at 18:48
  • ok so you are saying that I have to put it between function user(...) and return{...} right? – pp94 Sep 30 '14 at 18:49
  • @EricAlberson they are specific question. Basically I use the code just to ask the explanation of where I have to put my statement – pp94 Sep 30 '14 at 18:50

1 Answers1

3

First of all, if this is a constructor you should write it like this:

function User(_id, userName, email, password, firstName, lastName, date_created, playlists) {
    this._id = _id;
    this.userName = userName;
    this.email = email;
    this.password = password;
    this.firstName =  firstName;
    this.lastName =  lastName;
    this.date_created = date_created,
    this.playlists = playlists 
} 

This way, you can call it using the new keyword get a new object with the proper values set.

Next, in order to set default values you can do some shortfusing using || on each property. This way the default value is used only when the value is not provided.

function User(_id, userName, email, password, firstName, lastName, date_created, playlists) {
    this._id = _id;
    this.userName = userName;
    this.email = email;
    this.password = password;
    this.firstName =  firstName || "defaultValue";
    this.lastName =  lastName || "defaultValue";
    this.date_created = date_created || new Date(),
    this.playlists = playlists || [] 
} 
William Barbosa
  • 4,936
  • 2
  • 19
  • 37
  • Ok but if I have a default value I want to insert "William" and "Barbosa" how can I do it with your implementation? – pp94 Sep 30 '14 at 18:59
  • If the user provides the names when he calls the function, the value provided is used. If the user supplies no value, "DefaultValue" is used. `new User(1, "willkuns", "foo@bar.baz", "asd", "William")` will set the first name to "William" and the last name to "defaultValue", because only the first name was provided. – William Barbosa Sep 30 '14 at 19:01
  • Ok. I change my code because the one I inserted is the only that works but I don't understand why the if statements does not works!!! – pp94 Sep 30 '14 at 19:11
  • Not all falsy values are undefined. Check [this](http://www.sitepoint.com/javascript-truthy-falsy/) – William Barbosa Sep 30 '14 at 19:17