/**
* OO – Exercise
*
* 1. Collection object
* a. It should have a "add" method, which accepts an Item instance
* b. It should have a "remove" method, which removes the provided Item
* c. It should have a "size" method, which returns the size of internal data
* d. It should have a "contains" method, which returns a boolean indicating
* whether the Item exists in internal data
* 2. Item object
* a. It should have a "get" method that accepts a property name
* b. It should have a "set" method that accepts a property name and value
* 3. General
* a. We should be able to create multiple instances of Item and store them
* in one or more instances of Collection
* b. Each instance should be distinct and independent from other instances
* of the same type
* 5. BONUS
* a. Create a property on Item called "stores", that is an instance of
* Collection. When an Item is added to or removed from a Collection,
* it will update this property with the Collection instance
* b. Make all data accessible only through exposed methods
* c. If you are feeling particularly ambitious, abstract any common logic
* to a parent object, and inherit from it
*/
function Collection(id) {
this.id = id;
this.data = [];
}
Collection.prototype.add = function (itemToAdd) {
if (!this.contains(itemToAdd)) {
if (itemToAdd.stores) {
itemToAdd.stores.add(this);
}
this.data.push(itemToAdd);
}
};
Collection.prototype.contains = function (item) {
return this.data.indexOf(item) !== -1;
};
Collection.prototype.each = function (fn) {
this.data.forEach(fn);
};
Collection.prototype.filter = function (fn) {
return this.data.filter(fn);
};
Collection.prototype.remove = function (itemToRemove) {
if (this.contains(itemToRemove)) {
if (itemToRemove.stores) {
itemToRemove.stores.remove(this);
}
this.data =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.