OO – Exercise
by Shane Porter
JavaScript
/**
* OO – Exercise
*
* 1. Collection object
* a. Internal data should not be accessible, except through exposed methods
* b. It should have a "add" method, which accepts an Item instance
* c. It should have a "remove" method, which removes the provided Item
* d. It should have a "size" method, which returns the size of internal data
* e. It should have a "contains" method, which returns a boolean indicating
* whether the Item exists in internal data
* 2. Item object
* a. Internal data should not be accessible, except through exposed methods
* b. It should have a "get" method that accepts a property name
* c. 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. If you are feeling particularly ambitious, abstract any common logic
* to a parent object, and inherit from it
*/
function Collection(id) {
// implement me
}
function Item(id) {
// implement me
}
var collection1 = new Collection('collection1');
var collection2 = new Collection('collection2');
var item1 = new Item('item1');
var item2 = new Item('item2');
var item3 = new Item('item3');
item1.set('name', 'foo');
item2.set('name', 'bar');
item3.set('name', 'baz');
collection1.add(item1);
collection1.add(item2);
collection2.add(item2);
collection2.add(item3);
// blank previous console output
console.clear();
// collection 1 tests
console.assert(collection1.size() === 2, 'collection1 incorrect size!');
console.assert(collection1.contains(item1), 'collection1 hould...