如下所示:
復(fù)制代碼 代碼如下:
//擴(kuò)展對(duì)象的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());
新聞熱點(diǎn)
疑難解答
圖片精選