When you cast any object to a boolean value you get true
if the object is not null
and false
otherwise, I like to change this behaviour for some objects. I want that some objects return false
even when they aren't null
I know that in ActionScript 3.0 we can change some default behaviours of an object using Proxy
. Can we do the same for Boolean(object)
or object as Boolean
? And how this can be done?
I want to ask this after the next thought:
I have this code:
if (someObject)
someObject.DoSomething();
That's mean that DoSomething
is only called if someObject
is not null
but that is only because the "real" code behind that is this:
if (Boolean(someObject) == true)
someObject.DoSomething();
And works because any object is automatically casted to Boolean and the result is true
, but if the reference points to null
the result is false
.
I want to know if I can change THAT behaviour without adding a new function like isTrue(someObject)
or something like that.
Thanks in advance, and sorry for my poor English.
No, CustomClass(object) and as
are cast operators, behavior of both is defined by language. And Boolean(object) is, afaik, global function with language-defined behavior (and there is no function overloading.) There is no class-level operator for type-casting in ActionScript 3.0, you have to implement some property for true/false checks.
A word of warning: Proxy and Prototype are both very slow. And they make your app less easy to maintain as any additional programmer will need to take into account the unusual behavior of otherwise core clases in your app. This can lead to hard to find and clear bugs. So warngings aside, I don't think Proxy is what you want. I think what you want is The Prototype. http://tobyho.com/Modifying_Core_Types_in_ActionScript_3_Using_the_Prototype_Object
But seriously, you should just make a function that test for the classes you want to evaluate as false, instead of using the Boolean evaluator. Make a function that returns a boolean.
function isFalse(object:*):Boolean {
if(object == false){
return false;
} else if (object is SpecialcaseClass){
// perform special case test here. Return the value;
return resultOfSpecialCaseTest;
}
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With