獲得Javascript對象屬性個數的示例代碼
更新時間:2013年11月21日 09:13:17 作者:
這篇文章主要是對獲得Javascript對象屬性個數的示例代碼進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
如下所示:
//擴展對象的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());
相關文章
小發(fā)現之淺談location.search與location.hash的問題
下面小編就為大家?guī)硪黄“l(fā)現之淺談location.search與location.hash的問題。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

