A LUA-style table implementation that allows for arbitrary data types for keys.
Note
- Table should only be used when it's really important to use non-scalar types as keys, otherwise use plain objects, which have a better performance.
Syntax
var myTable = new Table();
Arguments
None
Returns
- (object) A new Table instance.
Set a value for a specific key. Note that the key can be anything - a function, an object, an element, etc.
Syntax
myTable.set(key, value);
Arguments
- key - (object) The key for the value; any type of object.
- value - (object) The value for the key; any type of object.
Returns
- (object) This Table instance.
Examples
var myTable = new Table();
myTable.set($('myForm'), {foo: 'bar'});
myTable.set(myClassInstance, $$('.someElements'));
Get a value for a specific key.
Syntax
myTable.get(key);
Arguments
- key - (object) The key for the value.
Returns
- (object) the value set for the specified key.
Erase a given key/value from the Table instance.
Syntax
myTable.erase(key);
Arguments
- key - (object) The key for the value.
Returns
- (object) This Table instance.
Iterates over the key/values in the table.
Syntax
myTable.each(function, bind);
Arguments
- function - (function) Function executed for each key/value pair in the Table instance; passed two arguments (key and value).
- bind - (object, optional) The object to be used as 'this' in the function. For more information see Function:bind.
Returns
- (object) This Table instance.
Examples
var myTable = new Table();
// first set some values
myTable.set($('myForm'), {foo: 'bar'});
myTable.set(myClassInstance, $$('.someElements'));
// and now iterate over them
myTable.each(function(value, key){
console.log(value, key);
});