简体   繁体   English

哪种方法可以检查在as3中持有布尔值的对象变量?

[英]which way is good to check the object variable holding boolean value in as3?

        var myString:Object = "true";
        if (myString.toLowerCase() == "true")

or 要么

        if (myString is Boolean)

or 要么

        if (myString == Boolean)

Which one is good to check the object variable is holding boolean values (true or false). 检查对象变量是保持布尔值(对还是错)是哪一种是好的。 Or which one is right? 还是哪个是对的?

Thanks... 谢谢...

The short answer is : 简短的答案是:

the first instruction : if (myString.toLowerCase() == "true") . 第一条指令: if (myString.toLowerCase() == "true")


The long answer is : 长答案是:

still the first instruction : if (myString.toLowerCase() == "true") . 仍然是第一条指令: if (myString.toLowerCase() == "true")

because you are really checking if your string is equals to "true" , while in the 2nd instruction you are checking if your string is a Boolean , and in the 3rd one your checking if your string is the Boolean class. 因为您实际上是在检查字符串是否等于"true" ,而在第二条指令中,您正在检查字符串是否是Boolean ,在第三条指令中,您正在检查字符串是否是Boolean类。

Hope that can help. 希望能对您有所帮助。

Honestly, your example code doesn't make much sense: you have an Object variable, yet you assume it is a String by using toLowerCase() , yet you check if it's a Boolean , too. 老实说,您的示例代码没有多大意义:您拥有一个Object变量,但您通过使用toLowerCase()假定它为String ,但您也检查它是否为Boolean If you know it's a string, make your variable a String : 如果您知道这是一个字符串,请将您的变量String

var myString:String = "true";

If you don't know it's a string, you need to check if it's a string, or else you will get an error when if it's not a string: 如果您不知道它是字符串,则需要检查它是否是字符串,否则如果不是字符串,则会出现错误:

if (myObject is String && (myObject as String).toLowerCase() == "true")

If it could be a boolean, you should check it as a boolean, too: 如果它可能是布尔值,也应该将其检查为布尔值:

else if(myObject is Boolean && (myObject as Boolean) == true)

In general, you would be much better off having some kind of decoding process somewhere before this code so that you know what kind of data you are dealing with, and comparisons are straight forward. 通常,在此代码之前的某处进行某种解码处理会更好一些,这样您就可以知道要处理的数据类型,并且比较也很简单。 For example, JSON.parse() of a JSON data structure will result in actual boolean values for boolean, and you can decode it into typed objects with properties of type Boolean . 例如,JSON数据结构的JSON.parse()将产生布尔值的实际布尔值,并且您可以将其解码为具有Boolean类型的属性的类型化对象。 The example code you have here is very fragile. 您在此处的示例代码非常脆弱。

Good luck. 祝好运。

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

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