Updating JavaScript: ES5

Jeff Walden

Mozilla Corporation

JavaScript, the standard

JavaScript is based on ECMAScript as specified by ECMA-262. Current browsers implement the third edition from 1998. The recent fifth edition is coming to browsers now.

What's New in ES5?

ES5's features are broadly broken into three areas: meta-object programming, a strict mode, and standard library enhancements. I'll briefly run through what each entails.

"Meta-Object Programming"

First, meta-object programming. In JavaScript objects are sets of properties, plus a little stuff. Meta-object programming is just examining and changing all that stuff.

Properties in ES3

ES3 user-defined properties come in only one flavor: they show up in loops, they can be changed, and they can be deleted.

Properties in ES5 (1)

In ES5 you can choose whether a property is enumerable, whether it can't be changed, and whether it can be deleted.

Properties in ES5 (2)

You can also define properties which compute a value on access, or which have side effects when set.

Objects in ES5

As for objects, you can fix their properties in place to prevent additions (and with property adjustments you can prevent deletions or value changes).

Introspection

Of course, all this is up to be inspected at runtime.

Strict Mode

Strict mode's the next big thing. If you opt into it, you get protection against many JavaScript warts and gotchas, and in time we'll be able to perform extra optimizations on strict-mode code.

Strict Mode Effects (1)

Octal escapes are gone because leading-zero, while sensible to programmers, is unexpected by the average Joe looking to align his decimals. Unintentional globals are pernicious scope-poisoning, good riddance. Property setting in ES3 can silently fail, but ES5 strict will throw then.

Strict Mode Effects (2)

with is gone because any name used inside the corresponding block — any name at all — must go through a slow two-step name-lookup process that kills most name-based optimizations. Preventing global object leakage is good for preventing surprises, and it's useful security-wise as well. Stack introspection, while convenient during debugging, means stack info has to be kept around or made computable for every function call, which makes compiling JS to native code more difficult.

Strict Mode Effects (3)

Did you even know you could have duplicate names like this? There's more beyond this, but screen space, complexity, and time mean I have to punt discussing those restrictions.

Standard Library Improvements

Last but not least, the standard library: new methods, properties, etc. that are things you could "mostly" implement in JS if you tried. They've been moved into the standard mostly for convenience and (sometimes) to get the extra bit needed to proceed past "mostly". (For full details examine the standard.) Everybody loves JSON. Mozilla's long-standing array extras are also there. There are a couple date functions providing extra efficiency and a standardized formatting. Last, ES5 includes support for "bound" functions, whose this when called is always the same.

Mozilla ES5 Support

Now: what of the previous new functionality has Mozilla implemented? We have full support for property manipulation. Object lockdown is being worked on now. We have some support for introspection of all this, with more just around the corner. The syntax restrictions of strict mode are implemented. Some of the runtime checks are implemented; some aren't but are in the pipeline. Last, we've implemented all the standard library bits save for bound functions. They're coming soon, but they're slightly lower priority than the rest because you can "more or less" make your own bound functions already.

Fini

And that's all. Download a nightly, fool around with it, and have fun! See the spec or IRC if you have questions.