prototype.js分析 Enumerable

pluck() 要素のプロパティを抜き出す

<script>
Array.prototype._each = function(iterator){
	for(var i=0, l=this.length; i<l; i++){
		iterator(this[i]);
	}
};

Array.prototype.each = function(iterator){
	var index = 0;
	try{
		this._each(function(v){
			iterator(v, index++);
		});
	} catch(e){
		if(e != $break) throw e;
	}
};

Array.prototype.pluck = function(property){
	var rs = [];
	this.each(function(v, k){
		rs.push(v[property]);
	});
	return rs;
};

var $break = {};
var kekka = "hoge,moge,foo,bar".split(',').pluck('length');
console.log(kekka);
</script>