0

I need to set up two flags canPlayLive and isSubscribed to true or false accordingly to values of parameters sent when creating the object.

Basically when null undefined '' is passed I want the flag on false, otherwise on true.

Using the following code flags are always on true.

What am I doing wrong here?

function MyStation(id, chId, name, hdImg, subscribedFlag, liveEntryId, liveUrl, timeLists) {
    this.id = id;
    this.dataId = chId;
    this.name = name;
    this.imageUrl = hdImg;
    this.subscribedFlag = subscribedFlag;
    this.liveEntryId = liveEntryId === null || undefined || '' ? null : liveEntryId;
    this.liveUrl = liveUrl === null || undefined || '' ? null : liveUrl;
    this.timeLists = timeLists;
    this.canPlayLive = this.liveUrl === null || undefined || '' ? false : true;
    this.isSubscribed = subscribedFlag == 0 ? false : true;
}

var test = new MyStation(
0,
'x123',
'foo',
'url',
0,
null,
'',
[]
);

console.log(test);
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • 1
    Read up on JS syntax. You need to do `liveEntryId === null || liveEntryId === undefined || or liveEntryId ===''`, but wouldn't `!liveEntryId` work just as well? –  Sep 16 '14 at 10:32
  • Agreed - `!liveEntryId` would cover all bases. – Mitya Sep 16 '14 at 10:33
  • 1
    ```!liveEntryId``` erroneously results in ```true``` if it is equal to ```0``` or ```NaN``` – sahbeewah Sep 16 '14 at 10:35
  • 1
    To be fair, so would `liveEntryId === null || liveEntryId === undefined || or liveEntryId ===''` – Evan Knowles Sep 16 '14 at 10:40
  • @sahbeewah - incorrect: both `0` and `NaN` are falsies, i.e. resolve to false when coerced to a boolean state. – Mitya Sep 17 '14 at 10:37
  • @Utkanos Note the negation operator (!). That is the point I was trying to point out, and therefore is not a valid solution to the problem. – sahbeewah Sep 17 '14 at 22:18

2 Answers2

1

Since canPlayLive is dependent on liveUrl you should write your code as follows:

if (liveUrl ) {
   this.liveUrl = (liveUrl == '') ? null : liveUrl;
}

Explanation: when parameter liveUrl is null or undefined the result will always be false otherwise true. Since you want that also empty string will be considered as null we need the second condition.

When this.liveUrl having the correct value let's go to canPlayLive variable:

 this.canPlayLive = this.liveUrl || false;

Explanation: When this.liveUrl is null it is considered as false, hence the result will be false.

when this.liveUrl is NOT null it is considered as true, hence true or false will always give us true.

Scription
  • 646
  • 3
  • 12
  • 21
0

I was able to solve my problem using !liveUrl instead of ''.

function MyStation(id, chId, name, hdImg, subscribedFlag, liveEntryId, liveUrl, timeLists) {
    this.id = id;
    this.dataId = chId;
    this.name = name;
    this.imageUrl = hdImg;
    this.subscribedFlag = subscribedFlag;
    this.liveEntryId = liveEntryId === null || undefined || !liveEntryId ? null : liveEntryId;
    this.liveUrl = liveUrl === null || undefined || !liveUrl ? null : liveUrl;
    this.timeLists = timeLists;
    this.canPlayLive = this.liveUrl === null || undefined || !liveUrl ? false : true;
    this.isSubscribed = subscribedFlag == 0 ? false : true;
}
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • useful: http://stackoverflow.com/questions/154059/how-do-you-check-for-an-empty-string-in-javascript – GibboK Sep 16 '14 at 10:59