Domanda

Voglio verificare nella mia funzione se un argomento passato di tipo oggetto è vuoto o meno. A volte è vuoto ma non è ancora nullo, quindi non posso fare affidamento su una condizione nulla. C'è qualche proprietà come 'lunghezza' / 'dimensione' per oggetti flex che posso usare qui. Per favore aiuto. Grazie in anticipo.

È stato utile?

Soluzione

Se vuoi dire se un oggetto non ha proprietà:

var isEmpty:Boolean = true;
for (var n in obj) { isEmpty = false; break; }

Altri suggerimenti

Questo è un trucco serio ma puoi usare:

Object.prototype.isEmpty = function():Boolean {
    for(var i in this)
        if(i != "isEmpty")
            return false
    return true
}

var p = {};
trace(p.isEmpty()); // true
var p2 = {a:1}
trace(p2.isEmpty()); // false

Puoi anche provare:

ObjectUtil.getClassInfo(obj).properties.length > 0

Il bello è che getClassInfo ti dà molte più informazioni sull'oggetto, ad es. ottieni i nomi di tutte le proprietà dell'oggetto, che potrebbero tornare utili.

Se l'oggetto contiene del 'testo' ma as3 non lo riconosce come stringa, convertilo in stringa e controlla se è vuoto.

var checkObject:String = myObject;

if(checkObject == '')
{
  trace('object is empty');
}

Dipende da cosa è il tuo oggetto, o piuttosto da cosa ti aspetti che abbia. Ad esempio, se si suppone che il tuo oggetto contenga alcune proprietà chiamate name che stai cercando, potresti farlo

if(objSomeItem == null || objSomeItem.name == null || objSomeItem.name.length == 0)
{
 trace("object is empty");
}

o se il tuo oggetto in realtà dovrebbe essere qualcos'altro, come un array che potresti fare

var arySomeItems = objSomeItem as Array;
if(objSomeItem == null || arySomeItems == null || arySomeItems.length == 0)
{
  trace("object is empty");
}

Puoi anche usare altri modi attraverso la riflessione, come ObjectUtil.getClassInfo, quindi enumerare le proprietà per verificare i valori impostati .... questa guida di classe:

import flash.utils.describeType;
import flash.utils.getDefinitionByName;

public class ReflectionUtils 
{
    /** Returns an Array of All Properties of the supplied object */
    public static function GetVariableNames(objItem:Object):Array
    {
        var xmlPropsList:XMLList = describeType(objItem)..variable;
        var aryVariables:Array = new Array();
        if (xmlPropsList != null)
        {
            for (var i:int; i < xmlPropsList.length(); i++)
            {
                aryVariables.push(xmlPropsList[i].@name);
            }
        }

        return aryVariables;
    }

    /** Returns the Strongly Typed class of the specified library item */
    public static function GetClassByName($sLinkageName:String):Class
    {
        var tObject:Class = getDefinitionByName($sLinkageName) as Class;
        return tObject;
    }

    /** Constructs an instance of the speicified library item */
    public static function ConstructClassByName($sLinkageName:String):Object
    {
        var tObject:Class = GetClassByName($sLinkageName);
        //trace("Found Class: " + tMCDefinition);
        var objItem:* = new tObject();
        return objItem;
    }

    public static function DumpObject(sItemName:String, objItem:Object):void
    {
        trace("*********** Object Dump: " + sItemName + " ***************");
        for (var sKey:String in objItem)
        {
            trace("    " + sKey +": " + objItem[sKey]);
        }
    }
    //}
}

Un'altra cosa da notare è che puoi usare un semplice ciclo per controllare le proprietà degli oggetti, ecco cosa sta facendo questa funzione dumpobject.

Puoi controllarlo direttamente come segue,

var obj:Object = new Object();
if(obj == null)
{
//Do something
}

L'ho rubato da una domanda simile relativa a JS. Richiede FP 11+ o una libreria JSON.as.

function isEmptyObject(obj){
    return JSON.stringify(obj) === '{}';
}

può usare il metodo hasProperty per verificare la lunghezza

var i:int = myObject.hasProperty("length") ? myObject.length: 0;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top