log.metatype

Object.prototype.length (Firefox only)

適当にlengthもgetterで書いてみた。

SnipplrObject.prototype.length (Firefox only)

Posted by inamorix on October 23rd, 2007

  1. Object.prototype.__defineGetter__('length', function () {
  2.         var cnt = 0;
  3.         for (var i in this) {
  4.                 if (this.hasOwnProperty(i)) cnt++;
  5.         }
  6.         return cnt;
  7. });

オブジェクト型の長さを返すだけ。

var obj = {
	a: 1,
	b: 2,
	c: 3,
	d: 4,
	e: 5
};
alert(obj.length);

あまり使う時無いかな?あるかな?

Post a comment