Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 17:13:35
Original
1103 people have browsed it

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());
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template