The ternary operator allow to conditionally evaluate two expressions (the first in case of the condition is true, the second one otherwise).
module.exports = global.document ?
factory( global, true ) :
function( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
};
The assignment means that, the result of the conditional evaluation is assigned to module.export
.
The ternary operator is a syntactic sugar and works like this:
function ternary_operator(condition, expression1, expression2) {
if (condition) {
return expression1;
} else {
return expression2;
}
}
so you can translate the code to:
module.exports = ternary_operator(global.document,
factory( global, true ),
function( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
});
Please, pay attention to:
factory( global, true )
and
function( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
};
They are both expressions: the result of the activation of the factory
function is an expression; the definition of a function value is an expression too.
Lazy evaluation
The function ternary_operator
provides an eager ternary operator. The expressions are evaluated within the condition, and then one of them are returned. For example:
var l = ternary_operator(C, A, B);
1. evaluate C
2. evaluate A
3. evaluate B
4. activate function ternary_operator
5. assign to l the result of the function
Javascript ternary operator is lazy: that means that the condition is firstly evaluated, then just one of the other expression is evaluatued, gaining in terms of performance.
var l = C ? A : B;
1. evaluate C
2.1 if C is true evaluate A
2.2 if C is false evalutate B
3. assign to l the expression evaluated