JavaScript计算对象长度
直接封装函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| function countObjectLength(obj) { var Length = 0; if ((typeof obj) == "string") { return obj.length; } else if ((typeof obj) == "object") { for (var key in obj) { if (obj.hasOwnProperty(key)) { Length++; } } return Length; } else { console.log("既不是字符串也不是对象无法计算长度"); return; } }
|
测试该函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| var a = {"name":"Lomo", "amy":"Sherry","age":"26"} alert(typeof a); alert(countObjectLength(a));
var b = [1,2,3,"lomo"]; alert(typeof b); alert(countObjectLength(b));
var c = 'lomoao'; alert(countObjectLength(c));
var arr = new Array("123","345","chnhawk","Sherry","a","b","c"); alert(countObjectLength(arr));
|
看起来,好像没毛病啊!!! 但是测试的对象里并不包含特殊情况,所以是存在问题的!
关于hasOwnProperty
hasOwnProperty()
方法返回的是一个bool型。如: a.hasOwnProperty(b)
, 判断a是否包含属性b, 属性b可能是个成员变量, 也可能是个成员方法.
在JavaScript中, 利用 hasOwnProperty()
方法去判断某个对象是否含有指定属性时, 在某些特殊情况下, 这个对象的属性名可能恰好使用的是这个关键字.
例如:
1 2 3 4 5 6 7 8 9 10 11
| var foo = { hasOwnProperty: function() { return false; }, bar: 'Here be dragons', lomo: "lo" }; foo.hasOwnProperty('bar'); foo.hasOwnProperty('hasOwnProperty'); foo.hasOwnProperty("lomo");
|
此时,使用开头的封装函数测试,也是无法得到正确结果的。
为什么?
因为:因为冲突,被覆盖!
解决办法:
使用外部原型链上的、或Object的hasOwnProperty属性方法去判断
1 2 3 4 5
| ({}).hasOwnProperty.call(foo, 'lomo');
Object.prototype.hasOwnProperty.call(foo, 'bar');
|
对开头封装的函数进行改进:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| function countObjectLength(obj) { var Length = 0; if ((typeof obj) == "string") { return obj.length; }else if ((typeof obj) == "object") { for (var key in obj) { if(({}).hasOwnProperty.call(obj, key)) { Length ++; } } return Length; }else { console.log("既不是字符串也不是对象无法计算长度"); return; } }
|
或
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| function countObjectLength(obj) { var Length = 0; if ((typeof obj) == "string") { return obj.length; }else if ((typeof obj) == "object") { for (var key in obj) { if(Object.prototype.hasOwnProperty.call(obj, key)) { Length ++; } } return Length; }else { console.log("既不是字符串也不是对象无法计算长度"); return; } }
|
测试
1 2 3 4 5 6 7 8 9
| var foo = { hasOwnProperty: function() { return false; }, bar: 'Here be dragons', lomo: "lo" };
countObjectLength(foo);
|
这两种函数都算是一个考虑很周(考虑到了特殊字符)的长度计算的最佳方法.