JavaScript 1.6 11

With the upcoming release of Firefox 1.5 Mozilla have decided to increase their JavaScript version number to 1.6. There are basically three new features since 1.5:

Array and String generics means that some of the methods that are available on Array.prototype and String.prototype are now also available as static methods of Array and String where the first argument passed is an obejct that has the same inteface. For strings it is not that useful becuase strings are immutable and all objects have a built in toString meathod already. But for array like structures it is really nice, allowing you to apply for example filter to a NodeList. Using this we can implement the oh-so-famous-getElementsByClassName with just a few lines of code:

Document.prototype.getElementsByClassName =
Element.prototype.getElementsByClassName = function (s) {
   return Array.filter(this.getElementsByTagName("*"), function (el) {
      return el.className.split(/\s+/).indexOf(s) != -1;
   });
};

window.onload = function () {
   Array.forEach(document.getElementsByClassName("foo"), function (el) {
      alert((new XMLSerializer).serializeToString(el));
   });
}

Personally I think there is room for a few more before finalizing 1.6.

  • http://www.challenger.se Hakan Bilgin

    Some usefull functions:

    Document.prototype.selectNodes = function($_XPath, $_xNode) {
    if(!$_xNode) $_xNode = this;
    oNSResolver = this.createNSResolver(this.documentElement);
    $_aItems = this.evaluate($_XPath, $_xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    $_aResult = [];
    for(i=0; i 0)? $_xItems[0] : null ;
    }
    Element.prototype.selectNodes = function($_XPath) {
    return this.ownerDocument.selectNodes($_XPath, this);
    }
    Element.prototype.selectSingleNode = function($_XPath) {
    return this.ownerDocument.selectSingleNode($_XPath, this);
    }
    Node.prototype.__defineGetter__(‘xml’, function() {
    return (new XMLSerializer()).serializeToString(this);
    });
    Node.prototype.__defineGetter__(‘outerHTML’, function() {
    return this.xml;
    });

    /* An extra function */
    Element.prototype.getElementsByClassName = function (s) {
    return this.selectNodes(“.//*[@class='"+ s +"']“);
    };

    All functions above apart from the last one are functions that I include in most of my work. These function are applied on HTML elements as well as XML Nodes. I find them useful :-P

    The last one, I just wrote this in this textarea and haven’t tested it but it should work (I don’t see why it would be useful…anywho) But it should do exactly as (Eriks) function above.

    Best regards,
    hbi

  • http://www.challenger.se Hakan Bilgin

    I dont know why but this function didn’t make it throught the submission (!?). One more try:

    Document.prototype.selectSingleNode = function($_XPath, $_xNode) {
    if(!$_xNode) $_xNode = this;
    $_xItems = this.selectNodes($_XPath, $_xNode);
    return ($_xItems.length > 0)? $_xItems[0] : null ;
    }

    /hbi

  • http://erik.eae.net Erik Arvidsson

    Those are useful extensions to Mozilla and I use them myself quite a lot. Using XPath for getElementsByClassName is a lot better than what I wrote but I wanted to show off Array.filter etc on NodeLists.

  • http://www.challenger.se Hakan Bilgin

    My point exactly… even though one has to extend Firefox with functions and objects to do arbitrary stuff, none of you pro-Firefox guys complain on the fact that they aren’t built in. Instead you blabber on with with the support of standards. But these and other methods, properties or objects should be built-in by default. This is the backside of Firefox and many developers has accustomed to having them automatically from IE. That is one of the reasons why its still more prefered.

    Why mimic a browser that you’re complaining about? Fact is that MS and IE didn’t affect the development of web and related in only negative ways. Few recognize this fact when focusing on and pointing at what they don’t like in MS.

    /hbi

  • http://erik.eae.net Erik Arvidsson

    “none of you pro-Firefox guys complain on the fact that they aren’t built in” – they are built in. You just do it in a different way (usually the W3C DOM way).

    “Why mimic a browser that you’re complaining about?” – because IE/JScript cannot support this kind of emulation. I would of course have preferred to fix the bugs in MSXML and IE and extend them to support DOM but that cannot be done.

    It all comes down to IE not being extensive and Mozilla being powerful enough to emulate everything in IE and MSXML. It is better to have one interface than two.

    I like MS :-)

  • http://www.challenger.se Hakan Bilgin

    How is “selectSingleNode” and “selectNodes” built in? I’m very curious, without any extension, how do you “selectNodes” with xPath? ‘Coz the other isn’t a issue…

  • http://erik.eae.net Erik Arvidsson

    var p = document.evaluate(“//p”, document, null, 9, null).singleNodeValue;

    A bit more cryptic but a lot more powerful. The W3C XPath DOM allows you to return more than just nodes.

    http://www.w3.org/TR/DOM-Level-3-XPath

  • http://dhtmlkitchen.com Garrett Smith

    Looks like I need to entitify my text. The “<” char got right through to the final output.

    // You should not have to check for a special null value.
    var matches = “foo”.match(/o/g);
    for(var i = 0; i < matches.length; i++) { … }

  • http://dhtmlkitchen.com Garrett Smith

    …I propose that:

    String.prototype match and RegExp.prototype.exec return arrays only, never null.

    Also very easy to implement would be:
    String.prototype:
    trim, startsWith, endsWith

    These are simple requests, much in line with your requests in this post, Erik. Nothing fancy like classes or modifiers, which would be great, but require significant changes to the language.

  • http://erik.eae.net Erik Arvidsson

    I agree that returning null for exec and match is a bit stupid. However, changing the return value might break existing code.

    Regarding the string methods, I believe you should file a bug to Bugzilla and cc Brendan and it might actually happen :-)

  • Hofneriu