JS Generics

With JavaScript 1.6 Mozilla introduced generic version of a lot of the Array and String methods. Without broad support these very useful methods are in reality only available to Firefox and Firefox extension. Personally I’ve become too used to them to survive long without them. I’ve previously posted the JS 1.5 array extras and I’ve also mentioned these methods but now I’ve implemented them for JS1.3 (JScript 5.6 and whatever they call the JS version they use in Opera)

js16generics.js

var arrayLike = {0: 3, 1: 2, 2: 1, 3: 0, length: 4};
Array.sort(arrayLike);
document.write(Array.join(arrayLike, "<br>"));

Demo

13 Responses to “JS Generics”

  1. M. Schopman Says:

    Makes me wonder, will something like this make it natively in Opera 9? Either way, nice job!

  2. Emil A Eklund Says:

    Nice one, will come in handy!

  3. Sebastian Werner Says:

    Wow. Cool. Could we include your generics implementation in our open-source (LGPL) toolkit qooxdoo (http://qooxdoo.oss.schlund.de)?

    Could please explain one thing I have found there:

    constr[methods[j]] = function (constr, name)
    {
    return function (s) {
    var args = Array.prototype.slice.call(arguments, 1);
    return constr.prototype[name].apply(s, args);
    };
    }(constr, name);

    What do the last line in this case? You define a function, but for what do you append this “(constr, name)”? Thank you for your help.

  4. Erik Arvidsson Says:

    I have now licensed the file using Apache Software License 2.0. This is compatible with GPL so you can use it in qooxdoo.

    The ugly syntax is because constr and name changes during the loop and the extra function actually binds the values to the right values (if this was not done all the methods would do this:

    function (s) {
    var args = Array.prototype.slice.call(arguments, 1);
    return Stringprototype["slice"].apply(s, args);
    }

    not what one wants ;-)

  5. nitro2k01 Says:

    Elegant solution! This is javascript the way I like it!

  6. Vincenzo Alcamo Says:

    The quote method has a bug: you have to replace \ with \\
    return ‘”‘ + this.replace(/\\/g, “\\\\”).replace(/\”/g, “\\\”") + ‘”‘;

    The quirk code can be made more readable in this way:
    function extend(constr, name) {
    return function (s) {
    var args = Array.prototype.slice.call(arguments, 1);
    return constr.prototype[name].apply(s, args);
    };
    }
    if (!constr[name]) { constr[name] = extend(constr, name); }

  7. Erik Arvidsson Says:

    Vincenzo: True and true

    I’ve now updated the script.

  8. snook.ca - a collection of tips, tricks and bookmarks in web development Says:

    An Interview with Dean Edwards…

    Dean Edwards was kind enough to answer a few questions on JavaScript. Tell me a bit of your background and how you got into JavaScript programming? I’ve been a programmer (professionally) for twenty years. Most of that time I’ve been……

  9. NoXi Says:

    apply( ) method is implemented in JavaScript 1.2, but the call( ) method is not implemented until JavaScript 1.5. according to the book “JavaScript: The Definitive Guide, 4th Edition”

  10. Erik Arvidsson Says:

    Call is part of ECMAScript edition 3 and all dialects of JS supports that (or otherwise they are not even worth using).

  11. Serge Says:

    Hi,
    I’m not as advanced as you are but I am seeing this page with Opera and it throws these errors:

    Inline script thread
    Error:
    name: TypeError
    message: Statement on line 128: Expression evaluated to null or undefined and is not convertible to Object: xmlHttp
    Backtrace:
    Line 128 of linked script http://erik.eae.net/playground/newpost/newpost.js
    xmlHttp.onreadystatechange = function ()
    {
    if (xmlHttp.readyState == 4)
    {
    self.onLoad();
    }
    }
    ;
    Line 105 of linked script http://erik.eae.net/playground/newpost/newpost.js
    this.reload();
    Line 98 of linked script http://erik.eae.net/playground/newpost/newpost.js
    this.setUri(sUri);
    Line 2 of inline#1 script in http://erik.eae.net/archives/2006/02/28/00.39.52/
    new NewPostChecker(”/playground/newpost/”);

    Inline script thread
    Error:
    name: TypeError
    message: Statement on line 33: Expression evaluated to null or undefined and is not convertible to Object: xmlHttp
    Backtrace:
    Line 33 of linked script http://erik.eae.net/playground/tunage/tunage.js
    xmlHttp.onreadystatechange = function ()
    {
    if (xmlHttp.readyState == 4)
    {
    self.onLoad();
    }
    }
    ;
    Line 10 of linked script http://erik.eae.net/playground/tunage/tunage.js
    this.reload();
    Line 28 of linked script http://erik.eae.net/playground/tunage/tunage-text.js
    t.setUri(”http://erik.eae.net/playground/tunage/?action=currentlyplaying”);
    Line 1 of inline#2 script in http://erik.eae.net/archives/2006/02/28/00.39.52/
    bindTunageToElement(”tunage-text”);

    Shouldn’t there be a way to avoid having these errors show in the javascript console?
    I may be a bit offbeat…

  12. Bisna Blog » Blog Archive » Bisna is nearby… Says:

    [...] Bisna grows so much that it’s not unrecognizable. A large set of JavaScript objects were created, some code is not my own creation, I can tell you; but the result is really impressive. Bisna pretends to be a new PHP framework in the future (and if I have enough time). Now it’s only intended to be the replacer for pAjax. So, it’s time to show you how the new code will look like: [...]

  13. crisp's blog » Blog Archive » Crossbrowser Array Generics Says:

    [...] would be much cooler if we could use those techniques in all modern browsers. Luckily Erik Arvidsson already gave us his version of JS Generics back in 2006(!) I also found a demonstration of crossbrowser generics in Dan Webb’s Scripting [...]

Leave a Reply