| re: How to implement collections (Set, List, Map) in JavaScript ?
Luke Matuszewski wrote:
[color=blue]
> I am writing a lot of code in JavaScript, and my code begins too look
> to complicated... My work would be more effective is i could somehow
> emulate Sets, Lists and Maps especially some of its operations.
>
> Set - A collection that contains no duplicate elements (and its
> elements may be everything: strings, function references, DOM element
> references etc.);[/color]
You are looking for any native ECMAScript object or a user-defined object
derived from it. You will need a setter, maybe using a hash function
applied on elements to be added to compute the respective property name,
to make sure no duplicate can be created within the data structure.
[color=blue]
> List - An ordered collection (also known as a sequence) (and again
> its elements may be everything: strings, function references, DOM
> element references etc.);[/color]
You are looking for Array objects, or any native ECMAScript object or a
user-defined object derived from it that refers to Array objects with
one of its properties. You will have to define a getter that considers
elements with a special value (for example `undefined') as an indicator
that the next/previous "list element" should be accessed instead.
[color=blue]
> Map - An object that maps keys to values. A map cannot contain
> duplicate keys; each key can map to at most one value;[/color]
You are looking for any native ECMAScript object, or a user-defined object
derived from it.
In all cases, unless said otherwise, I recommend to use Object objects as
prototype, because they provide the least number of built-in properties,
which minimizes the risk of a property name collision and the size of the
memory footprint of the user-defined object.
<URL:http://pointedears.de/scripts/collection.js> maybe helps you to
implement this.
PointedEars |