I ran across the following code in Ely Greenfield’s SuperImage from his Book component – I understand loader.load() but what does the rest of do?
loader.load((newSource is URLRequest)? newSource:new URLRequest(newSource));
It looks like some kind of crazy inline if statement but still, I’m a little preplexed. And if it is an if statement – is this way better than a regular if statement?
? is called the ‘ternary operator’ and it’s basic use is:
In this case, if newSource is a URLRequest, loader.load will be passed newSource directly, otherwise it will be passed a new URLRequest built from newSource.
The ternary operator is frequently used as a more concise form of if statement as it allows ifs to be inlined. The corresponding code in this case would be: