获得Javascript对象属性个数的示例代码
(编辑:jimmy 日期: 2025/5/11 浏览:3 次 )
如下所示:
复制代码 代码如下:
//扩展对象的count方法
Object.prototype.count = (
Object.prototype.hasOwnProperty(‘__count__')
) ? function () {
return this.__count__;
} : function () {
var count = 0;
for (var i in this) if (this.hasOwnProperty(i)) {
count ++;
}
return count;
};
//使用
var myObj = {
name1: “value1″,
name2: “value2″
};
alert(myObj.count());
复制代码 代码如下:
//扩展对象的count方法
Object.prototype.count = (
Object.prototype.hasOwnProperty(‘__count__')
) ? function () {
return this.__count__;
} : function () {
var count = 0;
for (var i in this) if (this.hasOwnProperty(i)) {
count ++;
}
return count;
};
//使用
var myObj = {
name1: “value1″,
name2: “value2″
};
alert(myObj.count());
下一篇:XMLHttpRequest处理xml格式的返回数据(示例代码)