检查对象是否为空

发布于 2024-08-08 20:36:46 字数 97 浏览 5 评论 0原文

我想检查我的函数传递的对象类型参数是否为空。有时它是空的但仍然不为空,因此我不能依赖空条件。是否有一些像“长度”/“大小”之类的柔性对象属性可以在这里使用。 请帮忙。 提前致谢。

I want to check in my function if a passed argument of type object is empty or not. Sometimes it is empty but still not null thus I can not rely on null condition. Is there some property like 'length'/'size' for flex objects which I can use here.
Please help.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

樱桃奶球 2024-08-15 20:36:46

如果您的意思是对象没有属性:

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

If you mean if an Object has no properties:

var isEmpty:Boolean = true;
for (var n in obj) { isEmpty = false; break; }
忱杏 2024-08-15 20:36:46

这是一些严重的黑客攻击,但你可以使用:

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

This is some serious hack but you can use:

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
想你的星星会说话 2024-08-15 20:36:46

您也可以尝试:

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

它的好处是 getClassInfo 为您提供有关该对象的更多信息,例如。您可以获得对象中所有属性的名称,这可能会派上用场。

You can also try:

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

The good thing about it is that getClassInfo gives you much more info about the object, eg. you get the names of all the properties in the object, which might come in handy.

一梦浮鱼 2024-08-15 20:36:46

如果对象包含一些“文本”,但 as3 无法将其识别为字符串,请将其转换为字符串并检查它是否为空。

var checkObject:String = myObject;

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

If object containes some 'text' but as3 doesn't recognize it as a String, convert it to string and check if it's empty.

var checkObject:String = myObject;

if(checkObject == '')
{
  trace('object is empty');
}
分开我的手 2024-08-15 20:36:46

取决于您的对象是什么,或者更确切地说您期望它具有什么。例如,如果您的对象应该包含您正在寻找的某个名为 name 的属性,您可能会这样做,

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

或者如果您的对象实际上应该是其他东西,比如您可以做的数组

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

您也可以通过反射使用其他方式,例如如 ObjectUtil.getClassInfo,然后枚举属性以检查设置值...此类帮助:

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]);
        }
    }
    //}
}

另一件事需要注意的是,您可以使用简单的 for 循环来检查对象属性,这就是此 dumpobject 函数正在做的事情。

Depends on what your object is, or rather what you expect it to have. For example if your object is supposed to contain some property called name that you are looking for, you might do

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

or if your object is actually supposed to be something else, like an array you could do

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

You could also use other ways through reflection, such as ObjectUtil.getClassInfo, then enumerate through the properties to check for set values.... this class help:

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]);
        }
    }
    //}
}

Another thing to note is you can use a simple for loop to check through an objects properties, thats what this dumpobject function is doing.

小姐丶请自重 2024-08-15 20:36:46

您可以直接检查如下,

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

You can directly check it as follow,

var obj:Object = new Object();
if(obj == null)
{
//Do something
}
一枫情书 2024-08-15 20:36:46

我从一个与 JS 相关的类似问题中偷来了这个。它需要 FP 11+ 或 JSON.as 库。

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

I stole this from a similar question relating to JS. It requires FP 11+ or a JSON.as library.

function isEmptyObject(obj){
    return JSON.stringify(obj) === '{}';
}
﹉夏雨初晴づ 2024-08-15 20:36:46

可以使用 hasProperty 方法来检查长度

var i:int = myObject.hasProperty("length") ? myObject.length: 0;

can use use the hasProperty method to check for length

var i:int = myObject.hasProperty("length") ? myObject.length: 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文