Over a million developers have joined DZone.

es6-shim – ECMAScript 6 functionality on ECMAScript 5

DZone's Guide to

es6-shim – ECMAScript 6 functionality on ECMAScript 5

· Web Dev Zone ·
Free Resource

Bugsnag monitors application stability, so you can make data-driven decisions on whether you should be building new features, or fixing bugs. Learn more.

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")
        > "hi".repeat(3)
  • Object.getOwnPropertyDescriptors() – makes Object.create() more useful.
        var copy = Object.create(Object.getPrototypeOf(orig),
        var newFoo = Object.create(FooProto,
                instanceProp1: 123,
                instanceProp2: "abc"
  • Object.is() – an improved version of === (which will likely become an operator called is in ECMAScript 6).
        > 0 === -0
        > Object.is(0, -0)
        > NaN === NaN
        > Object.is(NaN, NaN)
  • Map – gives you a dictionary with arbitrary keys.
        > var m = new Map();
        > m.set("1", "foo");
        > m.set(1, "bar");
        > m.get("1")
        > m.get(1)
    "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!
  • 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:

Take a look at the tests to get usage examples.


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

Monitor application stability with Bugsnag to decide if your engineering team should be building new features on your roadmap or fixing bugs to stabilize your application.Try it free.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}