Home > Articles > Open Source > Ajax & JavaScript

  • Print
  • + Share This
From the author of

The Object Model

Dart returns to a fairly Smalltalk-like object model, with some Java influences. There are a few notable differences, however. The first is that Dart includes a notion of constant objects. These must be constructible without side effects and so can be interned at compile time. If you create two constant objects with the same value, then they will be the same object.

More interesting is the idea of a factory constructor. JavaScript inherits a bit of brain damage from C++ via Java: Constructor functions are required to return the object that they were passed, which was created implicitly by the new keyword (or, in the case of C++, the new operator). This makes it impossible to transparently implement singletons, for example, or objects allocated from a cache. It also makes class clusters impossible.

In Smalltalk and Objective-C, there is no such thing as a constructor in the language; there is just a convention that the class method called new returns a new instance of the object. In Objective-C, it calls +alloc and then -init, so allocation and initialization are separated. In Smalltalk, the VM does the allocation, so this would not make sense.

In Dart, you can declare a constructor with the factory keyword. It will then be responsible for creating, as well as initializing, the object that it returns. It can, for example, return a different subclass depending on the values of the parameters.


When I started to look at Dart, I must admit that I was biased against it. On closer inspection, however, I think it does a lot of things right. It lacks some of the more powerful features of languages like Smalltalk and Self, but it retains the most commonly used subset and provides some very clean abstractions.

In particular, isolates are likely to ensure that Dart is useful for both client and server-side programming. They are roughly equivalent to Erlang processes and a little bit more explicitly separated than Go's goroutines, which can share data via aliased objects and global variables, as well as channels.

The class model, similarly, is more restrictive than Smalltalk, but more flexible than Java. The syntax is generally clean and avoids both the absolute minimalism of Self and Smalltalk and the kitchen sink incomprehensibility of Perl and its derivatives.

In short, Dart is a clean, yet unimaginative language. It fits into a similar place as Java. Like Java, I doubt it will be the favorite language for anyone involved in programming language design or research, but is a competent workhorse.

  • + Share This
  • 🔖 Save To Your Account