Skip to content

Instantly share code, notes, and snippets.

@lynxerzhang
Created April 8, 2013 10:01
Show Gist options
  • Save lynxerzhang/5335681 to your computer and use it in GitHub Desktop.
Save lynxerzhang/5335681 to your computer and use it in GitHub Desktop.
判断对象是否为空或者获取对象的长度是很常用的功能, 也许可以将这2个方法写入Object的prototype对象中, 方便调用。
var obj:Object = Object.prototype;
//获取对象长度
obj.getLength = function():int{
var len:int = 0;
for(var item:* in this){
len++;
}
return len;
}
//检查对象是否为空
obj.isEmpty = function():Boolean{
for(var item:* in this){
return false;
}
return true;
}
//将enumerable设为false, 可以避免在for...in和for...each中遍历到该动态添加的属性
Object.prototype.setPropertyIsEnumerable("getLength", false);
Object.prototype.setPropertyIsEnumerable("isEmpty", false);
//example
var d:Object = {"a":1, "b":2};
trace(d.getLength()); //2
trace(d.isEmpty()); //false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment