1

I don't even know where to start searching for the answer to this question, I've not seen syntax like this before!

function (_d) {
    this.xiSWFPath = !_d ? "expressinstall.swf" : _d;
    this.setAttribute("useExpressInstall", true);
}

does this equate to

this.xiSWFPath = if not (_d) [declared true, else false] then expressinstall.swf else _d

I appreciate this is a beginner question! where can I learn more about this syntax?

turtle342
  • 23
  • 3

3 Answers3

2

It's the ? : operator, which can trace its heritage back at least to C in the '70s.

test-expression ? expression1 : expression2

If the test expression evaluates to something truthy, then expression1 is evaluated and that's the value of the whole thing. Otherwise, it's expression2.

The operator binds pretty loosely, and left-to-right (beware, PHP programmers!). It's always safe and (often) more readable to use parentheses.

Note that this is part of the expression grammar. The construct can appear as part of any expression, anywhere. In your samples, it's the expression on the right-hand side of an assignment operation (as part of a var statement).

Pointy
  • 371,531
  • 55
  • 528
  • 584
  • 1
    +1. In this case, though, it's probably better written as `this.xiSWFPath = _d || "expressinstall.swf"`. – ruakh Jun 30 '13 at 21:15
  • @ruakh yes, that's indeed true! Perhaps the OP will run across that and post another question :-) – Pointy Jun 30 '13 at 21:15
0

The line in question this.xiSWFPath = !_d ? "expressinstall.swf" : _d; is equals to:

if (! _d ) {
    this.xiSWFPath = "expressinstall.swf";
} else {
    this.xiSWFPath = _d;
}

This link has more info about this shorthand for many languages: http://en.wikipedia.org/wiki/%3F:

Hope that helps.

Franco
  • 8,739
  • 7
  • 25
  • 32
0

What you are seeing is the Conditional or Ternary Operator ( try searching for that term and you will find lots.)

This expands to

if(!_d) 
  this.xiSWFPath = "expressioninstall.swf";
else 
  this.xiSWFPath = _d;

The if(!_d) is a bit more the checking for it being declared, it is saying if _d is falsy, or evaluates to false, quite a lot evuates to false in javascript.

if(_d in [undefined, null, 0, false, '', NaN]) //psydo-code does not work.

More informally if _d has not good value use rexpressioninstall.swf else use _d.

David Waters
  • 11,581
  • 7
  • 37
  • 74