开发者

is there a better way to check a value is not blank or null than this

开发者 https://www.devze.com 2023-02-04 07:27 出处:网络
I\'ve just taken over 开发者_如何学编程a project from another developer and the code has a lot of if statements like this:

I've just taken over 开发者_如何学编程a project from another developer and the code has a lot of if statements like this:

if( value != null || value != "" || value != undefined )
{
    doSomeThing();
}

Which while ok, does get a bit messy when used everywhere. Is there a better, neater way to write this? I was thinking of this

if( value.length > 0 || value != undefined )
{
    doSomeThing();
}

What do you all think?

Thanks

Stephen


It's nearly that, but with if( value.length > 0 || value != undefined ) when value is null value.length will thhrow a runtime error TypeError: Error #1009 as you are trying to access a property on a null value.

so you have to check before that the value is non null as for example :

if (value!=null&&value.length>0) {doSomeThing();}

or another form shorter :

if ((value||"").length>0) {doSomeThing();}

One note: Unless the type of value is *, it can't take the value undefined, it will be null.

var str:String=undefined;
trace(str==null); // will output true


I love to write statements more positive. The statements of the programmer checks if something is not null, but he means; if it exist:

In case of arrays or strings I use this:

 if ( value && value.length > 0 ) doSomething();

But in cases of objects or DisplayObjects I use this to check if it exist.

 if ( mc ) doSomething();

In most cases this is enough.


Here's some Timeline code for you to play with.

import flash.display.MovieClip;
import flash.display.Sprite;

var object : Object;
var array : Array;
var number : Number;
var integer : int;
var uinteger : uint;
var string : String;
var xml : XML;
var regexp : RegExp;
var movieClip : MovieClip;
var array2 : Array = [];
var object2 : Object = {};

trace( "uninstantiated Object =", object, "Boolean cast =", Boolean( object ) );
trace( "uninstantiated Array =", array, "Boolean cast =", Boolean( array ) );
trace( "uninstantiated Number =", number, "Boolean cast =", Boolean( number ) );
trace( "uninstantiated int =", integer, "Boolean cast =", Boolean( integer ) );
trace( "uninstantiated uint =", uinteger, "Boolean cast =", Boolean( uinteger ) );
trace( "uninstantiated String =", string, "Boolean cast =", Boolean( string ) );
trace( "empty String =", "", "Boolean cast =", Boolean( "" ) );
trace( "uninstantiated XML =", xml, "Boolean cast =", Boolean( xml ) );
trace( "uninstantiated RegExp =", regexp, "Boolean cast =", Boolean( regexp ) );
trace( "uninstantiated MoveClip =", movieClip, "Boolean cast =", Boolean( movieClip ) );
trace( "instantiated Array with length 0 XML =", array2, "Boolean cast =", Boolean( array2 ) );
trace( "instantiated Object with no properties =", object2, "Boolean cast =", Boolean( object2 ) );
trace( "uninstantiated index of instantiated array =", array2[4], "Boolean cast =", Boolean( array2[4] ) );
trace( "uninstantiated property of instantiated object =", object2[ "color" ], "Boolean cast =", Boolean( object2[ "color" ] ) );
try
{
//will throw an error
trace( "uninstantiated property of uninstatinated class =", movieClip.x, "Boolean cast =", Boolean( movieClip.x ) );  
}
catch ( e : Error ) {}

/*
output:

uninstantiated Object = null Boolean cast = false
uninstantiated Array = null Boolean cast = false
uninstantiated Number = NaN Boolean cast = false
uninstantiated int = 0 Boolean cast = false
uninstantiated uint = 0 Boolean cast = false
uninstantiated String = null Boolean cast = false
empty String =  Boolean cast = false
uninstantiated XML = null Boolean cast = false
uninstantiated RegExp = null Boolean cast = false
uninstantiated MoveClip = null Boolean cast = false
instantiated Array with length 0 XML =  Boolean cast = true
instantiated Object with no properties = [object Object] Boolean cast = true
uninstantiated index of instantiated array = undefined Boolean cast = false
uninstantiated property of instantiated object = undefined Boolean cast = false

As you can see, in general, you can just test for instantiation by using 
if ( variableName ) ...

But this will fail if you want to think of instantiated-but-empty Arrays and Objects
as false. Note that it will also fail, generating an error, if you check for properies
of uninitialized classes, e.g. 

var sprite : Sprite;
if ( sprite.x ) trace( "found x" ); //error, because sprite = null and null doesn't have an x property.

You can write a more liberal test like this:
*/

function exists( value : * ) : Boolean
{   
    var result : Boolean = true;
    if ( ! value ) return false;
    if ( value is Array && ! value.length ) return false;
    if ( value.constructor.toString() == "[class Object]" )
    {
        result = false;
        for each ( var prop in value ) { result = true; break; }
    }

    return result;
}

//tests

trace( "exists( object ) =", exists( object ) );
trace( "exists( array ) =", exists( array ) ); 
trace( "exists( number ) =", exists( number ) ); 
trace( "exists( integer ) =", exists( integer ) ); 
trace( "exists( uinteger ) =", exists( uinteger ) );
trace( "exists( string ) =", exists( string ) );
trace( "exists( '' ) =", exists( "" ) );
trace( "exists( xml ) =", exists( xml ) );
trace( "exists( regexp ) =", exists( regexp ) ); 
trace( "exists( movieClip ) =", exists( movieClip ) );
trace( "exists( array2 ) =", exists( array2 ) );
trace( "exists( object2 ) =", exists( object2 ) ); 
trace( "exists( array2[4] ) =", exists( array2[4] ) );
trace( "exists( object2[ 'color' ] ) =", exists( object2[ "color" ] ) );

var sprite : Sprite = new Sprite();
trace( "INSTANTIATED: exists( sprite ) = ", exists( sprite ) );

var object3 : Object = { color : 0xFFFFFF };
trace( "INSTANTIATED OBJECT WITH PROPERTY:  exists( object3 ) =", exists( object3 ) );

var array3 : Object = [ 1 ];
trace( "INSTANTIATED ARRAY WITH LENGTH:  exists( array3 ) =", exists( array3 ) );

/*
output:

exists( object ) = false
exists( array ) = false
exists( number ) = false
exists( integer ) = false
exists( uinteger ) = false
exists( string ) = false
exists( '' ) = false
exists( xml ) = false
exists( regexp ) = false
exists( movieClip ) = false
exists( array2 ) = false
exists( object2 ) = false
exists( array2[4] ) = false
exists( object2[ 'color' ] ) = false
INSTANTIATED: exists( sprite ) =  true
INSTANTIATED OBJECT WITH PROPERTY:  exists( object3 ) = true
INSTANTIATED ARRAY WITH LENGTH:  exists( array3 ) = true

One more note: be careful with ints and uints. They are always initialized. Whereas other variables
point to null, undefined or NaN until you give them values, ints and uints are automatically intialized
to zero. There is not way to have an int that doesn't point to a value:

var num : Number;
var i : int;
trace( num ); // NaN
trace( i ); //0

In general, that's okay, because 0 == false:

var i : int;
if ( i ) trace( "false" ); //false

But I've mixed myself up before with code like this:

var color : int = getColorValueFromXML();
if ( color ) drawCircleWithColor( color );

In the above code, I only want to draw a circle if color is initialized. 
However, color will ALWAYS bee initialized, to zero if not to anything else.

But what if getColorValueFromXML() returns 0x00000. We think of that as the
color black. But I see a black circle, because 0x000000 is also the same
as 0. And, in a conditional, 0 parses as false:

if ( 0x000000 ) drawCircleWithColor(); //drawCircleWithColor won't run!
*/
0

精彩评论

暂无评论...
验证码 换一张
取 消