....
내가 항목 프로토 타입 (
note that there are no classes in javascript)가 될 것이라고 생각 것, 그리고 이런 식으로 뭔가 보일 것
:
function Item(id, categoryId, data, rank) {
this.id = id;
this.categoryId = categoryId;
this.data = data;
this.rank = rank;
}
function Items() {
this.items = [];
this.findByCategory = function(categoryId) {
var result = [];
for(var i=0;i<this.items.length;i++) {
if (categoryId == this.items[i].categoryId)
result.push(this.items[i]);
}
return result;
}
this.add = function(id, categoryId, data, rank) {
this.items.push(new Item(id, categoryId, data, rank));
}
}
var items = new Items();
items.add(2, 0, null, null);
items.add(1, 1, null, null); // I'm not going to care about data and rank here
items.add(2, 1, null, null);
items.add(3, 1, null, null);
items.add(4, 2, null, null);
items.add(5, 3, null, null);
var cat1 = items.findByCategory(1);
alert(cat1); // you will get a result of 3 objects all of which have category 1
을
출처
2009-04-29 11:24:49
cgp