检查对象是否为空
我想检查我的函数传递的对象类型参数是否为空。有时它是空的但仍然不为空,因此我不能依赖空条件。是否有一些像“长度”/“大小”之类的柔性对象属性可以在这里使用。 请帮忙。 提前致谢。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您的意思是对象没有属性:
If you mean if an Object has no properties:
这是一些严重的黑客攻击,但你可以使用:
This is some serious hack but you can use:
您也可以尝试:
它的好处是 getClassInfo 为您提供有关该对象的更多信息,例如。您可以获得对象中所有属性的名称,这可能会派上用场。
You can also try:
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.
如果对象包含一些“文本”,但 as3 无法将其识别为字符串,请将其转换为字符串并检查它是否为空。
If object containes some 'text' but as3 doesn't recognize it as a String, convert it to string and check if it's empty.
取决于您的对象是什么,或者更确切地说您期望它具有什么。例如,如果您的对象应该包含您正在寻找的某个名为 name 的属性,您可能会这样做,
或者如果您的对象实际上应该是其他东西,比如您可以做的数组
您也可以通过反射使用其他方式,例如如 ObjectUtil.getClassInfo,然后枚举属性以检查设置值...此类帮助:
另一件事需要注意的是,您可以使用简单的 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
or if your object is actually supposed to be something else, like an array you could do
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:
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.
您可以直接检查如下,
You can directly check it as follow,
我从一个与 JS 相关的类似问题中偷来了这个。它需要 FP 11+ 或 JSON.as 库。
I stole this from a similar question relating to JS. It requires FP 11+ or a JSON.as library.
可以使用 hasProperty 方法来检查长度
can use use the hasProperty method to check for length