JavaScript: Array and Matrix Initialization, Sorting Dictionaries and Objects - included to GIST.JS

Two new functions made it into gist-lib, the successor of the technical JavaScript library is-lib, which I created during my stay at the excellent Systems Biology Group MoSys. The purpose of the library is the provision of a minimal set of functions, commonly used during the creation of technical web-applications. Technical being the emphasis. For instance, the client-based loading of remote files into a web-application, and subsequent value-filtering. There are some choices provided by web-framework libraries or PHP.JS, but these quickly bulk up the project's size. Underscore.js is a recent alternative that follows a similar idea.

1st Function: Dictionary / Object sorting in JavaScript:

Nested Objects are JavaScript's go-to variant for dictionary-implementations. Unfortunately, JavaScript has no comfortable, PHP-like way of sorting nested objects. See the StackOverflow Question: Sort a dictionary (or whatever key-value data structure in js) on word_number keys efficiently


One possible implementation is provided below, and will be followed up with a usage example.


2nd Function: Initializing Arrays and Matrices in JavaScript:

JavaScript does not allow straightforward array initialization, but in fact comes with its share of pitfalls. The prototypical example being: [Array(0), Array('0')]  . In this instance, the first array would have a length of 0, whilst the second one would have a length of 1. At that point, no explanation may be necessary anymore other than pointing out that the number supplied to Array.prototype.constructor(/*Int*/ i) sets the length attribute of the array, and allows the JIT compiler to optimize memory allocation on the current stack, yet the elements remain uninitialized. That may not be a bad idea, given that JavaScript only has two base values: undefined and null.

To initialize arrays the following function is useful:


GIST.JS will be released by the end of 2013.
LihatTutupKomentar