" /> Francis Cheng: May 2008 Archives

« April 2008 | Main | July 2008 »

May 15, 2008

ECMAScript 4 Object Initializers and Type Annotations

My previous post was about ECMAScript 4 object initializers and fixtures. In this post, I'm going to talk about another new feature of ECMAScript 4 object initializers: type annotations. That's right, in ES4 you'll be able to place type annotations on object initializers. To recap, an object initializer, a.k.a. an object literal, is often used in ActionScript 3 instead of the new operator to create an instance of the Object class. For example:

var techWriter:Object = {fname:"Francis", lname:"Cheng", company:"Adobe"};

In ActionScript 3.0, you can use object initializers exclusively with the Object class. Once this new feature of ES4 gets rolled in to ActionScript, however, you will be able to use object initializers with other classes. Here's how it will probably work: place a type annotation after the closing curly brace. Let's say I have a simple class named Employee:

class Employee {
  var fname;
  var lname;
  var company;
}
With ECMASCript 4, I'll be able to create an instance of Employee using an object initializer:
// ECMAScript 4th Edition code
var techWriter:Employee = {fname:"Francis", lname:"Cheng", company:"Adobe"}:Employee;

One thing to keep in mind is that this will only work if all the fields of the object literal are of the form "fieldName:value". So what's the point of including this capability? The rationale comes from the idea of "evolutionary programming", which refers to the idea of moving from a more informal, scripting-based way of programming to a more structured and formal way of programming. Say you start with a simple script that provides rudimentary interactivity on your web page. As time goes by, you may decide to add more features to the script. At some point, you may find that the script is getting so complex that some formal structure would help. The idea of evolutionary programming is that the language should help you transition from the informal style to the formal style.

Type annotations on object initializers is an example of a feature that facilitates the transition from informal to formal programming style. In the example above, you can see how I moved from using a simple instance of the Object class to the more formal structure of an Employee class. Another feature that helps with this type of transitional programming is structural types, which allows you to create an simple type without creating a class. To give you a better sense of what evolutionary programming means, let's step through the transition again, but this time with a structural type thrown into the mix.

First, I start with an instance of the generic Object class:

var techWriter:Object = {fname:"Francis", lname:"Cheng", company:"Adobe"};

Second, I decide to get a little more formal by creating a structural type to represent employees:

// ES4 structural type definition
type Employee = {fname:String, lname:String, company:String}

Third, I can now create instances of the Employee type with an object initializer or with the new operator:

// ES4 object initializer
var techWriter:Employee = {fname:"Francis", lname:"Cheng", company:"Adobe"}:Employee;

// ES4 new operator can be used with structural types
var techWriter2:Employee = new Employee("Francis", "Cheng", "Adobe")

Finally, I can replace the structural type with a class:

class Employee {
  var fname;
  var lname;
  var company;
}

So both structural types and type annotations on object initializers provide flexibility in the transition from using plain Object instances to instances of a class. For example, after I define the Employee class, there's no need for me to rewrite the instantiation code I wrote in step three because the code works for both the structural type and the class.

Here's a list of relevant links: