Updating JavaScript: ES5
Jeff Walden
Mozilla Corporation
JavaScript, the standard
- ECMA-262 3rd edition (ES3, 1998)
- ECMA-262 5th edition (ES5, 2009)
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?
- Meta-object programming
- Strict mode
- Standard library enhancements
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"
- Huh?
- Objects are sets of properties, plus some stuff
- Examining/changing all that stuff
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)
- Choose your flavor:
var o = {};
Object.defineProperty(o, "foo",
{ value: 17,
enumerable: false,
configurable: false,
writable: false });
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)
- Getters and setters
var p = { get y() { return "y"; },
_z: 2,
get z() { return this._z; },
set z(v) { this._z = v; } };
Object.defineProperty(p, "prop",
{ get: function() { return 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
- ...and of course there are functions to examine all this
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)
- No
with
statements
- No global object leakage via
this
(this
isn't converted to the global object)
- No stack introspection via
caller
/callee
/fun.arguments
(modern JIT engines cry)
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
- JSON
- Array
extras
Date.now()
numeric timestamp
- Dates support
toISOString
for simple serialization
- Bound functions with fixed-value
this
when called
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
- Meta-object
- Full property manipulation (incl. getters/setters) supported
- Object lock-down coming soon
- Property introspection partially implemented
- Strict mode
- Syntactic restrictions supported
- Partial runtime-restriction support
- Standard library
- Everything but bound functions (coming soon)
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.