Home >Web Front-end >JS Tutorial >Sample code to get the number of properties of a Javascript object_javascript skills

Sample code to get the number of properties of a Javascript object_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 17:13:351153browse

As shown below:

Copy code The code is as follows:

//Extend the count method of the object
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;
};

//Use
var myObj = {
name1: “value1″,
name2: “value2″
};

alert(myObj.count());
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn