Skip to content

From smalltalk to javascript and back

NicolasPetton edited this page Nov 25, 2011 · 12 revisions

Smalltalk ♥ JavaScript

Amber maps one to one with the JavaScript equivalent:

  • String ⇔ String
  • Number ⇔ Number
  • BlockClosure ⇔ function
  • Dictionary ⇔ Object
  • Error ⇔ Error
  • etc.

##Smalltalk ⇒ JavaScript Amber makes it easy to send messages to Smalltalk objects from JavaScript:

  1. Unary messages begin with an underscore: yourself becomes _yourself()
  2. Binary messages are prefixed with 2 underscores: 3@4 becomes (3).__at(4)
  3. Keyword message follow the same rules as unary messages but with single underscores and a final underscore: aDictionary at: 3 put: 4 becomes aDictionary._at_put_(3, 4)

Smalltalk classes are added to the global smalltalk JavaScript object: Object new becomes smalltalk.Object._new();.

##JavaScript ⇒ Smalltalk

You can also access JavaScript objects directly from Amber and access their properties or call functions:

  1. someUser.name becomes someUser name
  2. someUser name = "John" becomes someUser name: 'John' console.log('hello world') becomes console log: 'hello world'
  3. window.jQuery('foo').css('background', 'red') becomes (window jQuery: 'foo') css: 'background' color: 'red'

For keyword selectors, only the first keyword matters, all the other keywords will be ignored. That means our previous example could have been: (window jQuery: 'foo') css: 'background' set: 'red'