Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
HTML5 Zone is brought to you in partnership with:

Dr. Axel Rauschmayer is a freelance software engineer, blogger and educator, located in Munich, Germany. Axel is a DZone MVB and is not an employee of DZone and has posted 228 posts at DZone. You can read more from them at their website. View Full User Profile

es6-shim – ECMAScript 6 functionality on ECMAScript 5

12.28.2011
Email
Views: 1149
  • submit to reddit
The HTML5 Microzone is presented by DZone and Microsoft to bring you the most interesting and relevant content on emerging web standards.  Experience all that the HTML5 Microzone has to offer on our homepage and check out the cutting edge web development tutorials on Script Junkie, Build My Pinned Site, and the HTML5 DevCenter.
Paul Miller’s es6-shim gives you functionality that will be in ECMAScript 6 (code-named ECMAScript.next), on ECMAScript 5 engines. It was initially based on a project of mine, but adds much new functionality, Node.js compatibility, and (not least) tests.

A few highlights:

  • Strings
        > "hello world".startsWith("hello")
        true
        > "hi".repeat(3)
        'hihihi'
    
  • Object.getOwnPropertyDescriptors() – makes Object.create() more useful.
        var copy = Object.create(Object.getPrototypeOf(orig),
            Object.getOwnPropertyDescriptors(orig));
    
        var newFoo = Object.create(FooProto,
            Object.getOwnPropertyDescriptors({
                instanceProp1: 123,
                instanceProp2: "abc"
            }));
    
  • Object.is() – an improved version of === (which will likely become an operator called is in ECMAScript 6).
        > 0 === -0
        true
        > Object.is(0, -0)
        false
        > NaN === NaN
        false
        > Object.is(NaN, NaN)
        true
    
  • Map – gives you a dictionary with arbitrary keys.
        > var m = new Map();
        undefined
        > m.set("1", "foo");
        undefined
        > m.set(1, "bar");
        undefined
        > m.get("1")
        'foo'
        > m.get(1)
        'bar'
    
    "1" and 1 are (coerced to) the same key with arrays. Note that each object is considered different from any other object. Hence, the following map entry cannot be easily retrieved:
        > m.set({}, "hello");
        > m.get({})  // new object!
        undefined
    
  • Easy install on Node.js (you must run at least Node.js 0.6.5!):
        npm install es6-shim
    
    Afterwards, you enable it in your project like this:
        require("es6-shim");
    

Take a look at the tests to get usage examples.

 

Source: http://www.2ality.com/2011/12/es6-shim.html

Published at DZone with permission of Axel Rauschmayer, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

HTML5 is the most dramatic step in the evolution of web standards. It incorporates features such as geolocation, video playback and drag-and-drop. HTML5 allows developers to create rich internet applications without the need for third party APIs and browser plug-ins.  Under the banner of HTML5, modern web standards such as CSS3, SVG, XHR2, WebSockets, IndexedDB, and AppCache are pushing the boundaries for what a browser can achieve using web standards.  This Microzone is supported by Microsoft, and it will delve into the intricacies of using these new web technologies and teach you how to make your websites compatible with all of the modern browsers.