简体   繁体   English

loader.load((newSource is URLRequest)?newSource:new URLRequest(newSource))到底是什么? 做?

[英]What the heck does loader.load((newSource is URLRequest)? newSource:new URLRequest(newSource)); do?

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? 我从Ely Greenfield的 Book组件中查看了Ely Greenfield的 SuperImage中的以下代码-我了解loader.load(),但是其余的工作是什么?

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. 看起来有些疯狂的内联if声明,但我还是有些困惑。 And if it is an if statement - is this way better than a regular if statement? 如果它是一个if语句-这种方法是否比常规的if语句更好?

? is called the 'ternary operator' and it's basic use is: 被称为“三元运算符”,其基本用法是:

(expression) ? (evaluate to this if expression is true) : (evaluate to this otherwise);

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. 在这种情况下,如果newSource是URLRequest,则将直接将loader.load传递给newSource,否则将传递从newSource构建的新URLRequest。

The ternary operator is frequently used as a more concise form of if statement as it allows ifs to be inlined. 三元运算符通常用作if语句的更简洁形式,因为它允许内联if。 The corresponding code in this case would be: 在这种情况下,相应的代码为:

if (newSource is URLRequest)
   loader.load(newSource);
else
   loader.load(new URLRequest(newSource));

Basically what it says is: if newsource is a type of URLRequest, then pass the newSource variable into the load method, if its not a type of URLReuqest, create a new URLRequest and pass that into the load method. 基本上说的是:如果newsource是URLRequest的一种,则将newSource变量传递给load方法,如果它不是URLReuqest的类型,则创建一个新的URLRequest并将其传递给load方法。

The basic syntax is: (condition) ? 基本语法是:(条件)? (code to execute if true) : (code to execute if false) (如果为true,则执行代码):(如果为false,则执行代码)

this is using the ternary ?: operator . 这是使用三元?:运算符 the first part is the condition, between the ? 第一部分是条件,介于? and : is what to return if the condition is true. 和:是条件为真时返回的内容。 after the : is what to return if the condition is false. 如果条件为假,则返回:之后是什么。

a simpler example 一个简单的例子

String str = null;
int x = (str != null) ? str.length() : 0;

would be the same as 将与

String str = null;
int x;
if (str != null)
  x = str.length()
else
  x = 0;

Basically what this means, as far as im aware of, is its asking wheter that variable newSource's class is String or URLRequest like workmad and jason explained. 就我所知,基本上,这意味着它要问的是变量newSource的类是String还是URLRequest,就像workmad和jason解释的那样。 If its an URLRequest it will run loader.load(newSource:URLRequest) . 如果是URLRequest,它将运行loader.load(newSource:URLRequest) If its not an URLRequest it means automatically that it is a String (in other words the url). 如果不是URLRequest,则自动表示它是一个字符串(即url)。 And in that case it will run loader.load(new URLrequest(newSource:String) . 在这种情况下,它将运行loader.load(new URLrequest(newSource:String))

The complete code could look something like this: 完整的代码如下所示:

function myFunction(newSource:Object):SomeClass {
var loader:URLLoader = new URLLoader();
loader.load((newSource is URLRequest)? newSource:new URLRequest(newSource));
}

Regards, 问候,

Filipe A. 菲利普·A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM