获取对象长度
Object.prototype.length = function len() {
let count = 0;
for(let i in this) {
count++;
}
return count - 1;// -1的原因是对象中默认包含__proto__属性,获取对象真实的长度需要减去1.
};
例子:
let obj = {a: 1,b: 2, c: 3};
console.log(obj.length()) // 3;
Object.prototype.length = function len() {
let count = 0;
for(let i in this) {
count++;
}
return count - 1;// -1的原因是对象中默认包含__proto__属性,获取对象真实的长度需要减去1.
};
例子:
let obj = {a: 1,b: 2, c: 3};
console.log(obj.length()) // 3;