My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
OList  
Overview of OList
Updated Nov 16, 2010 by matr...@mac.com

OList

A class that provides an OO object for managing collections of other objects.

Usage

var list = new OList([ type ]);

type (optional) The objx class that this list will contain.

Properties

items

The internal array containing the items in the list.

constraints

An object containing the criteria which objects must meet in order to be accepted into the list.

Methods

add

Adds an item (or many items) to the list.

list.add( obj1 [, obj2] );

clear

Clears the list.

list.clear();

item

Gets or sets the item at the given index.

list.item( index [, newItem] );

index The index of the item to get or set
newItem (optional) The item to place at that index.

// getting the item at index 1
list.item(1);

// setting the item at index 1
list.item(1, "World");

size

Gets or sets the size of the list.

list.size( [newSize ]);

newSize (optional) The new size of the list.

// getting the size
var size = list.size();

// setting the size (trimming the list)
list.size(5);

// clearing the list
list.size(0);

Examples

A generic list

The 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 list

You 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 list

If 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 error

Why not just use an array?

  • OList's type safety is a very useful tool in object oriented programming.

Sign in to add a comment
Powered by Google Project Hosting