« Flash Player Public Bugbase Introduced | Main | ECMAScript 4 Object Initializers and Type Annotations »

Object Initializers and Fixtures for ECMAScript 4th Edition

A recently released ECMAScript 4 draft specification for object initializers includes significant enhancements to the expressive power of object initializers. This post focuses on the ability to designate specific properties as fixtures, which is not possible in ECMAScript 3rd edition or ActionScript 3.0.

You may have used object initializers in ActionScript 3.0 before. They provide an easy way to assign a literal value to a variable of type Object. For example, in ActionScript 3.0, you may see something like:

This creates an object called myObj with two dynamic properties, name and color. The properties are dynamic in that they are not defined as part of the Object class and can be deleted at runtime. For example, the following code deletes the name property:

In ActionScript 3.0, if you want to ensure that the name property cannot be deleted at runtime, you have to define a class and make name a member, or "trait", of that class. In ActionScript 3.0, a trait is a class member that cannot be deleted at runtime. In ECMAScript 4, traits are called "fixtures", but you don't have to create a class definition to create a fixture. In fact, you can create fixtures with object initializers. For example, let's say you want to make the name property a fixture, so that runtime code cannot delete the property. In ES4, you'll be able to do this:

What if you want both name and color to be fixtures? All you have to do is place the "var" keyword before the curly braces. Doing so will make every property you name a fixture, like so:

The fun doesn't stop there, though. Let's say you like creating objects on the fly like this, but that you'd want each object to have a unique ID number property that not only is a fixture, but also is a constant (i.e. read-only). All you have to do is use the keyword const instead of var:

You can also use the const keyword before the curly braces if you want to make all of the properties you name read-only fixtures.

Here's a list of relevant links:

Comments

Yeah Thanks for the information about ECMA4 :) It seems to be very powerful. Does that will impact ActionScript performances ?

Francis,
Will we see a performance gain by leveraging fixtures in the new language? One of the comments from Jim Corbett and Lee Thomason's presentation on the AVM at MAX mentioned that using dynamic objects requires the AVM to lookup the property to determine if it exists and then what the value is. By leveraging fixtures will this help streamline the VM and treat it more like a sealed class or is this more to help implement structure with dynamic objects?

I think that you should see similar performance improvements whether you create fixtures with object initializers or with classes. As I understand it, what's happening behind the scenes in my examples above is that anonymous subclasses of Object are created. That would suggest to me that fixtures are fixtures, regardless of how they are created.

Thanks for keeping us up to date an some of the neat proposals for the ES4 spec. I'm looking forward to seeing how this affects actionscript, but for now it gives me some interesting food for thought.

I definitely like the "const" on the object. That's pretty sweet (not sure of applicability yet but looks potentially useful).

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)