Class: ObjectSet

hormigas. ObjectSet

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

Properties:
Name Type Description
hormigas.ObjectSet.superConstructor
Source:
  • maria.js, line 1346

<readonly> size

The number of elements in the set.

Source:
  • maria.js, line 1348

Methods

<static> mixin(obj)

Mixes in the ObjectSet methods 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
obj Object

The object to become an ObjectSet.

Source:
  • maria.js, line 1642

['delete'](element) → {boolean}

If element is in the set then removes element from the set.

delete is a reserved word and older implementations did not allow bare reserved words in property name position so quote delete.

var alpha = {};
var set = new hormigas.ObjectSet(alpha);
set['delete'](alpha); // true
set['delete'](alpha); // false
Parameters:
Name Type Description
element Object

The item to delete from the set.

Source:
  • maria.js, line 1424
Returns:

true if element is deleted from the set as a result of this call. Otherwise false because element was not in the set.

Type
boolean

add(element) → {boolean}

If element is 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); // false
Parameters:
Name Type Description
element Object

The item to add to the set.

Source:
  • maria.js, line 1392
Returns:

true if element is added to the set as a result of this call. Otherwise false because element was 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:

true if elements were deleted from the set as the result of this call. Otherwise false because no elements were in the set.

Type
boolean

every(callbackfn, thisArg) → {boolean}

Calls callbackfn for 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;
}); // false
Parameters:
Name Type Argument Description
callbackfn function

The function to call for each element in the set.

thisArg Object <optional>

The object to use as the this object in calls to callbackfn.

Source:
  • maria.js, line 1523
Returns:

true if callbackfn returns a truthy value for all elements in the set. Otherwise false.

Type
boolean

forEach(callbackfn)

Calls callbackfn for 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
callbackfn function

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); // false
Parameters:
Name Type Description
element Object

The item in question.

Source:
  • maria.js, line 1373
Returns:

true if element is in the set. Otherwise false.

reduce(callbackfn, initialValue) → {*}

Calls callbackfn for each element of the set.

For the first call to callbackfn, if initialValue is supplied then initalValue is the first argument passed to callbackfn and 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 to callbackfn. 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); // 10
Parameters:
Name Type Description
callbackfn function

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 callbackfn for 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;
}); // true
Parameters:
Name Type Argument Description
callbackfn function

The function to call for each element in the set.

thisArg Object <optional>

The object to use as the this object in calls to callbackfn.

Source:
  • maria.js, line 1553
Returns:

true if callbackfn returns a truthy value for at least one element in the set. Otherwise false.

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