|
OList
Overview of OList
OListA class that provides an OO object for managing collections of other objects. Usagevar list = new OList([ type ]);
PropertiesitemsThe internal array containing the items in the list. constraintsAn object containing the criteria which objects must meet in order to be accepted into the list. MethodsaddAdds an item (or many items) to the list. list.add( obj1 [, obj2] ); clearClears the list. list.clear(); itemGets or sets the item at the given index. list.item( index [, newItem] );
// getting the item at index 1 list.item(1); // setting the item at index 1 list.item(1, "World"); sizeGets or sets the size of the list. list.size( [newSize ]);
// getting the size var size = list.size(); // setting the size (trimming the list) list.size(5); // clearing the list list.size(0); ExamplesA generic listThe simplest way to use OList is to create an instance of it and start adding items. You may add any items to a list constructed with no arguments: var myList = new OList();
myList.add("Hello world");
myList.add(123);
myList.add({ name: "Mat" });A strict listYou can make a list strict by specifying the only objx class type that you want your list to accept as an argument in the constructor. var MyClass = OClass({});
var myList = new OList(MyClass);
var instance1 = new MyClass();
var instance2 = new MyClass();
// add both items
myList.add(instance1, instance2);If you try to add an object that is not of this type, you will receive an error. If ODebug is false, no checks will be performed. An even stricter listIf you wish to put more constraints onto the objects your list will accept, you can do so using the constraints property. For example, if you wanted your list to only accept objects representing people over 18, you could do this: var myStrictList = new OList();
myStrictList.constraints({ age: {gt: 18} });
myStrictList.add({ name: "Mat", age: 27 });
// will be fine
myStrictList.add({ name: "Ethan", age: 5 });
// will throw an errorWhy not just use an array?
| ||||||||