Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 509089 of util/list

  • Revision slug: Mozilla/Add-ons/SDK/Low-Level_APIs/util_list
  • Revision title: util/list
  • Revision id: 509089
  • Created:
  • Creator: wbamberg
  • Is current revision? No
  • Comment

Revision Content

Experimental

Building blocks for composing lists.

Globals

Constructors

Iterable()

Constructs an Iterable object.

List(element1, element2, ...)

Constructor can takes any number of elements and creates an instance of List populated with the specified elements.

Parameters

element1 : Object|String|Number

element2 : Object|String|Number

... : Object|String|Number

Iterable

Base trait that can be used to compose traits with non-standard enumeration behaviors.

This trait is supposed to be used as part of a composition, since it only provides custom enumeration behavior to a composed object. It defines one required _keyValueMap property, that is used as a hash of "key-values" to iterate on during enumeration.

Properties

_keyValueMap

Hash map of key-values to iterate over. Required property: that is, the property must be supplied by objects that compose this trait. Note: That this property can be a getter if you need dynamic behavior.

List

An ordered collection (also known as a sequence) disallowing duplicate elements. List is composed out of Iterable, therefore it provides custom enumeration behavior that is similar to array (enumerates only on the elements of the list).

List is a base trait and is meant to be part of a composition, since all of its API is private except for the length property.

Examples:

var MyList = List.compose({
  add: function add(item1, item2, /*item3...*/) {
    Array.slice(arguments).forEach(this._add.bind(this));
  },
  remove: function remove(item1, item2, /*item3...*/) {
    Array.slice(arguments).forEach(this._remove.bind(this));
  }
});
MyList('foo', 'bar', 'baz').length == 3;        // true
new MyList('new', 'keyword').length == 2;       // true
MyList.apply(null, [1, 2, 3]).length == 3;      // true
let list = MyList();
list.length == 0;                               // true
list.add(1, 2, 3) == 3;                         // true

Methods

_has(element)

Parameters

element : Object|Number|String
Returns true if this list contains the specified element.

_add(element)

Parameters

element : Object|Number|String
Appends the specified element to the end of this list, if it doesn't contain it.

Ignores the call if element is already contained.

_remove(element)

Parameters

element : Object|Number|String
Removes specified element from this list, if it contains it.

Ignores the call if element is not contained.

_clear()

Removes all of the elements from this list.

Properties

length

Number of elements in this list.

Revision Source

<div class="note">
 <p>Experimental</p>
</div>
<p><span class="seoSummary">Building blocks for composing lists.</span></p>
<h2 id="Globals">Globals</h2>
<h3 id="Constructors">Constructors</h3>
<h4 class="addon-sdk-api-name" id="Iterable()"><code>Iterable()</code></h4>
<p>Constructs an <code>Iterable</code> object.</p>
<h4 class="addon-sdk-api-name" id="List(element1.2C_element2.2C_...)"><code>List(element1, element2, ...)</code></h4>
<p>Constructor can takes any number of elements and creates an instance of <code>List</code> populated with the specified elements.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>element1 : Object|String|Number</strong></p>
<p><strong>element2 : Object|String|Number</strong></p>
<p><strong>... : Object|String|Number</strong></p>
<h2 id="Iterable">Iterable</h2>
<p>Base trait that can be used to compose traits with non-standard enumeration behaviors.</p>
<p>This trait is supposed to be used as part of a composition, since it only provides custom enumeration behavior to a composed object. It defines one required <code>_keyValueMap</code> property, that is used as a hash of "key-values" to iterate on during enumeration.</p>
<h3 id="Properties">Properties</h3>
<h4 class="addon-sdk-api-name" id="_keyValueMap"><code>_keyValueMap</code></h4>
<p>Hash map of key-values to iterate over. <em>Required</em> property: that is, the property must be supplied by objects that compose this trait. <em>Note: That this property can be a getter if you need dynamic behavior.</em></p>
<h2 id="List">List</h2>
<p>An ordered collection (also known as a sequence) disallowing duplicate elements. List is composed out of <code>Iterable</code>, therefore it provides custom enumeration behavior that is similar to array (enumerates only on the elements of the list).</p>
<p>List is a base trait and is meant to be part of a composition, since all of its API is private except for the <code>length</code> property.</p>
<p><strong>Examples:</strong></p>
<pre class="brush: js">
var MyList = List.compose({
  add: function add(item1, item2, /*item3...*/) {
    Array.slice(arguments).forEach(this._add.bind(this));
  },
  remove: function remove(item1, item2, /*item3...*/) {
    Array.slice(arguments).forEach(this._remove.bind(this));
  }
});
MyList('foo', 'bar', 'baz').length == 3;        // true
new MyList('new', 'keyword').length == 2;       // true
MyList.apply(null, [1, 2, 3]).length == 3;      // true
let list = MyList();
list.length == 0;                               // true
list.add(1, 2, 3) == 3;                         // true</pre>
<h3 id="Methods">Methods</h3>
<h4 class="addon-sdk-api-name" id="_has(element)"><code>_has(element)</code></h4>
<h5 id="Parameters">Parameters</h5>
<p><strong>element : Object|Number|String</strong><br />
 Returns <code>true</code> if this list contains the specified <code>element</code>.</p>
<h4 class="addon-sdk-api-name" id="_add(element)"><code>_add(element)</code></h4>
<h5 id="Parameters">Parameters</h5>
<p><strong>element : Object|Number|String</strong><br />
 Appends the specified <code>element</code> to the end of this list, if it doesn't contain it.</p>
<p><em>Ignores the call if <code>element</code> is already contained.</em></p>
<h4 class="addon-sdk-api-name" id="_remove(element)"><code>_remove(element)</code></h4>
<h5 id="Parameters">Parameters</h5>
<p><strong>element : Object|Number|String</strong><br />
 Removes specified <code>element</code> from this list, if it contains it.</p>
<p><em>Ignores the call if <code>element</code> is not contained.</em></p>
<h4 class="addon-sdk-api-name" id="_clear()"><code>_clear()</code></h4>
<p>Removes all of the elements from this list.</p>
<h3 id="Properties">Properties</h3>
<h4 class="addon-sdk-api-name" id="length"><code>length</code></h4>
<p>Number of elements in this list.</p>
Revert to this revision