How to implement collections (Set, List, Map) in JavaScript ?

  • Thread starter Luke Matuszewski
  • Start date
L

Luke Matuszewski

Hej everybody.

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.);

List - An ordered collection (also known as a sequence) (and again
its elements may be everything: strings, function references, DOM
element references etc.);

Map - An object that maps keys to values. A map cannot contain
duplicate keys; each key can map to at most one value;

Can anyone point me to relevant URL's ? I know it is a lot to ask at
one post, but any clues would help.

B.R.
Luke Matuszewski.
 
T

Thomas 'PointedEars' Lahn

Luke said:
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.);

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.
List - An ordered collection (also known as a sequence) (and again
its elements may be everything: strings, function references, DOM
element references etc.);

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.
Map - An object that maps keys to values. A map cannot contain
duplicate keys; each key can map to at most one value;

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top