forked from amber-smalltalk/amber
-
Notifications
You must be signed in to change notification settings - Fork 0
From smalltalk to javascript and back
siemenbaader edited this page Nov 29, 2013
·
12 revisions
Amber maps one to one with the JavaScript equivalent:
- String ⇔ String
- Number ⇔ Number
- BlockClosure ⇔ function
- Dictionary ⇔ Object
- Error ⇔ Error
- etc.
You can easily access JavaScript objects directly from Amber and access their properties or call functions:
-
someUser.name
becomessomeUser name
-
someUser.name = "John"
becomessomeUser name: 'John'
-
console.log('hello world')
becomesconsole log: 'hello world'
-
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'
Note that you can inspect, print, etc javaScript objects from Amber.
For example, you can inspect the window
object.
Amber also makes it easy to send messages to Smalltalk objects from JavaScript:
- Unary messages begin with an underscore:
yourself
becomes_yourself()
- Binary messages are prefixed with 2 underscores:
3@4
becomes(3).__at(4)
- Keyword messages follow the same rules as unary messages but with single underscores and a final underscore:
aDictionary at: 3 put: 4
becomesaDictionary._at_put_(3, 4)
Smalltalk classes are added to the global smalltalk
JavaScript object: Object new
becomes smalltalk.Object._new();
.
A Smalltalk instance variable is accessed from within JavaScript as self['@name']
.