Enhanced Scripting in IE9: ECMAScript 5 Support and More

Up to this point we have mostly talked about improved JavaScript performance in Internet Explorer 9 but we haven’t said much about any new or changed language features in the “Chakra” engine. Now, with the third Platform Preview, we can tell you about JavaScript feature enhancements which you can try for yourself.

As context, the industry standard that defines the JavaScript language is ECMA-262: ECMAScript Language Specification developed and published by Ecma International. Prior to last year, it had been a decade since the introduction of the Third Edition of ECMA-262 in December 1999. In December 2009 Ecma approved the Fifth Edition of ECMA-262 as the successor to the Third Edition (a Fourth Edition was never published), and last year we debuted elements of ECMAscript 5 (ES5) support when we added native JSON support in IE8. Beyond JSON, though, ES5 standardizes many significant enhancements to the JavaScript language.

New ES5 Features in the IE9 Platform Preview

There are many important ES5 features implemented in IE9 Standards Document mode:

New Array Methods. There are nine new methods that operate upon arrays. Two of them, indexOf and lastIndexOf, support searching an array for a particular value. They are similar in concept to the functions with the same names that operate upon strings. The other seven new Array methods allow arrays to be manipulated using a functional programming style. For example, the following snippet uses the new filter method to collect the elements of an array that meet a specific condition:

//a function that tests whether menu item object is enabled or disabled
function enabled(menuItem) {return menuItem.status==="enabled"};

//Assume that individual menu items have a status property and
//that a menu object has a items property which is an array.
//Create an new array containing just the enabled menu items
var enabledItems=myMenu.items.filter(enabled);

These methods support various forms of array processing without having to explicitly code for loops. In addition, they are all generic, which means that they can be applied to any object with numerically indexed properties and not just objects created using the Array constructor. You can explore demos using these methods on the IE9 Test Drive site and they are summarized in the following table:

Array method

Description

indexOf

Search an array for the first occurrence of some value

lastIndexOf

Search an array for the last occurrence of some value

forEach

Apply a function to each element of an array

every

Determine if some condition is true for every element of an array

some

Determine if some condition is true for at least one element of an array

map

Apply a function to each element of an array and produce a new array containing the results

filter

Collect into a new array all the elements of an array for which some condition is true.

reduce

Accumulate a single value based upon all elements of an array.

reduceRight

Accumulate a single value based upon all elements of an array, processing them in reverse order.

Enhanced Object Model. The most important new feature in this area is accessor properties. These are also sometimes called “getter/setter” properties because they allow JavaScript programmers to control what happens when the program gets or sets the property value. ES5’s enhanced object model also allows programmers to control whether individual properties can have their value changed, are enumerated by for-in statements, and whether or not the property can be deleted or redefined. It also allows the programmer to control whether new properties can be added to an object. ES5 also enables JavaScript programmers to more easily create objects that inherit from specific prototype object and to inspect and manipulate the property definitions of object. All of these enhanced object model capabilities are accessible via new function properties of the Object constructor. However, note that the current release of the IE9 platform preview does not yet fully support use of these methods with DOM objects.

Object function

Description

Object.defineProperty

Create or modify a property definition. The property can be defined as either a data or an accessor property and its writable, enumerable, and configurable property attributes can be set.

Object.defineProperties

Create or modify multiple property definitions in a single operation.

Object.create

Create a new object with a specified prototype and optionally a set of specified properties.

Object.getPrototypeOf

Retrieve the prototype object of the argument object.

Object.getOwnPropertyDescriptor

Return a complete description of the attributes of a property of an object.

Object.getOwnPropertyDescriptor

Return an array containing the names of all of an object’s non-inherited properties.

Object.keys

Return an array containing the names of all of an object’s non-inherited properties that would be iterated by the for-in statement.

Object.seal

Disallow adding any additional properties to the argument object and disallow deletion or redefinition of any existing properties. Individual property values may still be modified if their writable attribute have the value true.

Object.freeze

Disallow adding any additional properties to the argument object and disallow deletion or redefinition of any existing properties. In addition the values of existing property may not be modified.

Object.isSealed

Test whether an object is has been sealed using Object.seal.

Object.isFrozen

Test whether an object is has been frozen using Object.freeze.

Object.preventExtensions

Disallow adding any additional properties to an object.

Object.isExtensible

Test whether new properties may be added to an object.

Other Computational Methods and Functions. In addition to the new Array and Object methods, ES5 adds or enhances several additional methods that perform useful computational operations.

Method or Function

Description

String trim

Removes “white space” from the beginning and end of a string.

Date toISOString

Convert a Date to a string format that all ES5 implementations must support.

Date.parse

Existing function enhance to recognize the format create by toISOString.

Date.now

Return a numeric timestamp

Array.isArray

Reliably test whether an object is an Array

Function bind

Preset some of the arguments of a function to fixed values.

ES5 also includes a number of other minor changes and technical corrections to the language. Many have no impact on most JavaScript programmers because they simply standardize minor features that have always been supported by browsers. An example of such a feature is line continuations within string literals. One minor change is of more interest. Reserved names such as if, super, and public can now be used as property names within object literals and for property access following a dot. With this change, programmers no longer need to worry about a long and arbitrary list of words that they can’t easily use as property names.

“Same Script, Same Markup”

Updating IE9’s JavaScript implementation isn’t just about supporting new ES5 features. It’s also about ensuring that web developers can use the same markup and script within IE9 that they use in other browsers. Earlier this year we released documents that describe in detail how JavaScript as implemented in IE8 differs from the ECMAScript, Third Edition Specification. In IE9 standards mode, we looked closely at these differences and made changes to ensure that IE9 can execute the same script as other browsers.



Corrected Issue

Example

Function expressions were processed as if they were function declarations.

function f() {alert("declaration")};
obj.callback=function f() {alert("expression")};
f(); // IE8 incorrectly alerts "expression"

Function names in function expressions were not locally defined within the function body.


var fact="the web is big";
Math.factorial=function fact(n)
{return n
 
Back
Top