JS Generics 13

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 thoughts on “JS Generics

  1. M. Schopman Feb 28,2006 00:57

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

  2. Emil A Eklund Feb 28,2006 12:21

    Nice one, will come in handy!

  3. Sebastian Werner Mar 1,2006 05:04

    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 Mar 2,2006 00:37

    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 Mar 3,2006 03:39

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

  6. Vincenzo Alcamo Mar 16,2006 02:53

    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 Mar 17,2006 03:03

    Vincenzo: True and true

    I’ve now updated the script.

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

  9. NoXi Mar 21,2006 06:50

    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 Mar 21,2006 10:33

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

  11. Serge Mar 21,2006 22:12

    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. Pingback: Bisna Blog » Blog Archive » Bisna is nearby…

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

Comments are closed.