Smalltalk Classes in JavaScript

February 2, 2011

Smalltalk is a class-based language and JavaScript is a prototype-based language.

It would be nice if objects in JavaScript had “class” values, but they don’t.

Try this in Firebug (Firefox plugin), Chrome’s console, or any other JavaScript interactive environment:


Numbers don’t know their class.

So, let’s fix that:

Number.prototype.class = ‘I am a Smalltalk number’

Now try the example above again:

‘I am a Smalltalk number’

And it is not just “123” that now has a class. *Every* number now thinks that it is a Smalltalk number.

‘I am a Smalltalk number’

‘I am a Smalltalk number’

You can do the same for Strings, Arrays, and Functions:

String.prototype.class = ‘I am a Smalltalk string’
Array.prototype.class = ‘I am a Smalltalk array’
Function.prototype.class = ‘I am a Smalltalk block’

Now try these:

‘Smalltalk is better than JavaScript’.class
‘I am a Smalltalk string’

‘I am a Smalltalk array’

(_=function(x) {return x*x;}).class
‘I am a Smalltalk block’

Of course, putting a string as the class value isn’t very useful.
In QuickSilver “.class” points to a Smalltalk “class” object.

BTW, don’t do the following:

Object.prototype.class = ‘I am an Object’

Changing the “Object” prototype can mess up a number of JS libraries.

However, you can set “class” for individual objects:

x = {}
x.class = ‘I am a blue widget’
‘I am a blue widget’

In practical Smalltalk terms this means that you can assign the class value while performing a “basicNew” operation to create a new instance.

So, with a little effort, you can make JavaScript objects have class values.


6 Responses to “Smalltalk Classes in JavaScript”

    • Peter Fisk Says:

      Thanks for the info.

      AFACT, JSTALK runs on the desktop using Swing and Rhino and produces JavaScript for the browser.
      OTOH, QuickSilver runs entirely in the client.

      — Peter

  1. Have you thought about import/export formats for classes, and aggregations of classes? Tradtional fileout seems nasty to me, as well as XML. I can’t find any references to anything else – I thought there was going to be something else. I’ve been out of the loop on Smalltalk for over a decade 😦

    Here’s something I’m playing with right now, for just plain old JavaScript:

    An example source file is here:

    Seems like it would be pretty straight-forward to do something similar, but for Smalltalk.

    • Peter Fisk Says:

      Scooj looks really interesting! I have downloaded it and will take a look as soon as I get a chance.

      Yes, I have thought about a different serialization scheme for classes and aggregations.

      I plan to use JSON as a general serialization format.

      The reason is very simple – JSON is already implemented in many places including virtually all server-side environments. PHP even has some very efficient encode_JSON, decode_JSON built-in functions.

      In the Google App Engine environment, I run Clojure Lisp server-side and it supports JSON as well.

      So all the “heavy lifting” stuff like code refactoring and library pruning will be done on the server – in PHP, Python, Groovy, Scala, Clojure, or …

      I see Smalltalk/QS as a client-side language with fast loading, good user event handling, and peer-to-peer or client-server messaging.

      • You’re not thinking big enough – we can do a lot in the browser itself. 🙂

        I consider JSON a binary format. It’s barely readable in it’s raw, and isn’t writable at all. It is great as a portable binary serialized format though. Something like scooj, which removes the horrors of brackets, braces, quotes and commas, would be much preferred, I think. Until we have such great browsers that we never have to look at “source code” any more. Will be a wee bit longer, I think.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: