new ObjectSet(item)
A constructor function for creating set objects. A set can only contain a particular object once. That means all objects in a set are unique. This is different from an array where one object can be in the array in multiple positions.
ObjectSet objects are designed
to hold JavaScript objects. They cache a marker on the objects.
Do not attempt to add primitives or host objects in a ObjectSet. This
is a compromise to make ObjectSet objects efficient for use in the model
layer of your MVC-style application.
When using the set iterators (e.g. forEach) do not depend
on the order of iteration of the set's elements. ObjectSet objects are unordered.
var set = new hormigas.ObjectSet(); // an empty set
ObjectSet objects have a size property that is the number of elements in the set.
var alpha = {};
var beta = {};
var set = new hormigas.ObjectSet(alpha, beta, alpha);
set.size; // 2
The methods of an ObjectSet object are inspired by the incomplete
Harmony Set proposal and the Array.prototype iterators.
Parameters:
| Name | Type | Argument | Description |
|---|---|---|---|
item |
Object |
<optional> |
An object to add to the set. |
- Source:
- maria.js, line 1334
Members
-
<static> superConstructor
-
- Source:
- maria.js, line 1346
Properties:
Name Type Description hormigas.ObjectSet.superConstructor -
<readonly> size
-
The number of elements in the set.
- Source:
- maria.js, line 1348
Methods
-
<static> mixin(obj)
-
Mixes in the
ObjectSetmethods into any object.Example 1
app.MyModel = function() { hormigas.ObjectSet.call(this); }; hormigas.ObjectSet.mixin(app.MyModel.prototype);Example 2
var obj = {}; hormigas.ObjectSet.mixin(obj);Parameters:
Name Type Description objObject The object to become an
ObjectSet.- Source:
- maria.js, line 1642
-
['delete'](element) → {boolean}
-
If
elementis in the set then removeselementfrom the set.deleteis a reserved word and older implementations did not allow bare reserved words in property name position so quotedelete.var alpha = {}; var set = new hormigas.ObjectSet(alpha); set['delete'](alpha); // true set['delete'](alpha); // falseParameters:
Name Type Description elementObject The item to delete from the set.
- Source:
- maria.js, line 1424
Returns:
trueifelementis deleted from the set as a result of this call. Otherwisefalsebecauseelementwas not in the set.- Type
- boolean
-
add(element) → {boolean}
-
If
elementis not already in the set then adds element to the set.var alpha = {}; var set = new hormigas.ObjectSet(); set.add(alpha); // true set.has(alpha); // falseParameters:
Name Type Description elementObject The item to add to the set.
- Source:
- maria.js, line 1392
Returns:
trueifelementis added to the set as a result of this call. Otherwisefalsebecauseelementwas already in the set.- Type
- boolean
-
clear() → {boolean}
-
If the set has elements then removes all the elements.
var alpha = {}; var set = new hormigas.ObjectSet(alpha); set.clear(); // true set.clear(); // false- Source:
- maria.js, line 1451
Returns:
trueif elements were deleted from the set as the result of this call. Otherwisefalsebecause no elements were in the set.- Type
- boolean
-
every(callbackfn, thisArg) → {boolean}
-
Calls
callbackfnfor each element of the set.var one = {value: 1}; var two = {value: 2}; var three = {value: 3}; var set = new hormigas.ObjectSet(one, two, three); set.every(function(element) { return element.value < 2; }); // falseParameters:
Name Type Argument Description callbackfnfunction The function to call for each element in the set.
thisArgObject <optional>
The object to use as the this object in calls to callbackfn.
- Source:
- maria.js, line 1523
Returns:
trueifcallbackfnreturns a truthy value for all elements in the set. Otherwisefalse.- Type
- boolean
-
forEach(callbackfn)
-
Calls
callbackfnfor each element of the set.var alpha = {value: 0}; var beta = {value: 1}; var gamma = {value: 2}; var set = new hormigas.ObjectSet(alpha, beta, gamma); set.forEach(function(element) { console.log(element.value); });Parameters:
Name Type Description callbackfnfunction The function to call for each element in the set.
- Source:
- maria.js, line 1495
-
has(element)
-
Is a particular object in the set or not?
var alpha = {}; var beta = {}; var set = new hormigas.ObjectSet(alpha); set.has(alpha); // true set.has(beta); // falseParameters:
Name Type Description elementObject The item in question.
- Source:
- maria.js, line 1373
Returns:
trueifelementis in the set. Otherwisefalse. -
reduce(callbackfn, initialValue) → {*}
-
Calls
callbackfnfor each element of the set.For the first call to
callbackfn, ifinitialValueis supplied theninitalValueis the first argument passed tocallbackfnand the second argument is the first element in the set to be iterated. Otherwise the first argument is the first element to be iterated in the set and the second argument is the next element to be iterated in the set.For subsequent calls to
callbackfn, the first argument is the value returned by the last call tocallbackfn. The second argument is the next value to be iterated in the set.var one = {value: 1}; var two = {value: 2}; var three = {value: 3}; var set = new hormigas.ObjectSet(one, two, three); set.reduce(function(accumulator, element) { return {value: accumulator.value + element.value}; }); // {value:6} set.reduce(function(accumulator, element) { return accumulator + element.value; }, 4); // 10Parameters:
Name Type Description callbackfnfunction The function to call for each element in the set.
initialValue* The optional starting value for accumulation.
- Source:
- maria.js, line 1596
Returns:
The value returned by the final call to
callbackfn.- Type
- *
-
some(callbackfn, thisArg) → {boolean}
-
Calls
callbackfnfor each element of the set.var one = {value: 1}; var two = {value: 2}; var three = {value: 3}; var set = new hormigas.ObjectSet(one, two, three); set.some(function(element) { return element.value < 2; }); // trueParameters:
Name Type Argument Description callbackfnfunction The function to call for each element in the set.
thisArgObject <optional>
The object to use as the this object in calls to callbackfn.
- Source:
- maria.js, line 1553
Returns:
trueifcallbackfnreturns a truthy value for at least one element in the set. Otherwisefalse.- Type
- boolean
-
toArray() → {Array}
-
Convert the set to an array.
- Source:
- maria.js, line 1468
Returns:
The elements of the set in a new array.
- Type
- Array