Initializing a new JavaScript object

65 views
Skip to first unread message

Tom

unread,
Feb 14, 2013, 9:56:05 AM2/14/13
to amber-lang
Hi everybody,

how comes, I cannot evaluate the following in the workspace...

jsObj := <{}>.

All I get is an alert() saying: "Parse error on line 1 column 17 :
Unexpected character <"

Any suggestions?

Apostolis Xekoukoulotakis

unread,
Feb 14, 2013, 10:04:05 AM2/14/13
to amber...@googlegroups.com
The new interpreter doesnt allow to have both smalltalk and javascript code at the same method or workspace.

2013/2/14 Tom <t...@tcltalk.ath.cx>

--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to amber-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.





--

Sincerely yours, 
     Apostolis Xekoukoulotakis

Apostolis Xekoukoulotakis

unread,
Feb 14, 2013, 10:06:12 AM2/14/13
to amber...@googlegroups.com
Use a HashedCollection object instead. I think it is exactly the same.

2013/2/14 Apostolis Xekoukoulotakis <xeko...@gmail.com>

Tom

unread,
Feb 14, 2013, 10:16:00 AM2/14/13
to amber-lang
You mean...

jsObj := #().
jsObj at: #x put: 23.

???

On 14 Feb., 16:06, Apostolis Xekoukoulotakis <xekou...@gmail.com>
wrote:
> Use a HashedCollection object instead. I think it is exactly the same.
>
> 2013/2/14 Apostolis Xekoukoulotakis <xekou...@gmail.com>
>
>
>
>
>
>
>
>
>
> > The new interpreter doesnt allow to have both smalltalk and javascript
> > code at the same method or workspace.
>
> > 2013/2/14 Tom <t...@tcltalk.ath.cx>
>
> >> Hi everybody,
>
> >> how comes, I cannot evaluate the following in the workspace...
>
> >> jsObj := <{}>.
>
> >> All I get is an alert() saying: "Parse error on line 1 column 17 :
> >> Unexpected character <"
>
> >> Any suggestions?
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "amber-lang" group.
> >> To unsubscribe from this group and stop receiving emails from it, send an
> >> email to amber-lang+...@googlegroups.com.
> >> For more options, visithttps://groups.google.com/groups/opt_out.

Herby Vojčík

unread,
Feb 14, 2013, 10:25:00 AM2/14/13
to amber...@googlegroups.com


Apostolis Xekoukoulotakis wrote:
> Use a HashedCollection object instead. I think it is exactly the same.

Not _exactly_ (HashedCollection is deeper subclass, it is not wrapping
plain JS Object), but it can be used in lots of places instead of plain
object (jQuery options, css etc., all works).

> 2013/2/14 Apostolis Xekoukoulotakis <xeko...@gmail.com
> <mailto:xeko...@gmail.com>>
>
> The new interpreter doesnt allow to have both smalltalk and
> javascript code at the same method or workspace.
>
>
> 2013/2/14 Tom <t...@tcltalk.ath.cx <mailto:t...@tcltalk.ath.cx>>
>
> Hi everybody,
>
> how comes, I cannot evaluate the following in the workspace...
>
> jsObj := <{}>.
>
> All I get is an alert() saying: "Parse error on line 1 column 17 :
> Unexpected character <"
>
> Any suggestions?
>
> --
> You received this message because you are subscribed to the
> Google Groups "amber-lang" group.
> To unsubscribe from this group and stop receiving emails from
> it, send an email to amber-lang+...@googlegroups.com
> <mailto:amber-lang%2Bunsu...@googlegroups.com>.

Herby Vojčík

unread,
Feb 14, 2013, 10:26:15 AM2/14/13
to amber...@googlegroups.com


Tom wrote:
> You mean...
>
> jsObj := #().
> jsObj at: #x put: 23.

jsObj := #{ 'x'->23. 'y'->42 }.
Use must use string, #x not supported (there is an issue for that).

Tom

unread,
Feb 14, 2013, 10:30:11 AM2/14/13
to amber-lang
Thanxalot :)

I should have mentioned earlier. I am looking for a way to copy/paste
some JSON Arrays into Smalltalk objects.

On 14 Feb., 16:25, Herby Vojčík <he...@mailbox.sk> wrote:
> Apostolis Xekoukoulotakis wrote:
> > Use a HashedCollection object instead. I think it is exactly the same.
>
> Not _exactly_ (HashedCollection is deeper subclass, it is not wrapping
> plain JS Object), but it can be used in lots of places instead of plain
> object (jQuery options, css etc., all works).
>
>
>
>
>
>
>
> > 2013/2/14 Apostolis Xekoukoulotakis <xekou...@gmail.com
> > <mailto:xekou...@gmail.com>>
>
> >     The new interpreter doesnt allow to have both smalltalk and
> >     javascript code at the same method or workspace.
>
> >     2013/2/14 Tom <t...@tcltalk.ath.cx <mailto:t...@tcltalk.ath.cx>>
>
> >         Hi everybody,
>
> >         how comes, I cannot evaluate the following in the workspace...
>
> >         jsObj := <{}>.
>
> >         All I get is an alert() saying: "Parse error on line 1 column 17 :
> >         Unexpected character <"
>
> >         Any suggestions?
>
> >         --
> >         You received this message because you are subscribed to the
> >         Google Groups "amber-lang" group.
> >         To unsubscribe from this group and stop receiving emails from
> >         it, send an email to amber-lang+...@googlegroups.com
> >         <mailto:amber-lang%2Bunsu...@googlegroups.com>.
> >         For more options, visithttps://groups.google.com/groups/opt_out.

Apostolis Xekoukoulotakis

unread,
Feb 14, 2013, 10:43:03 AM2/14/13
to amber...@googlegroups.com
Ok. Inspecting on both objects though gives a HashedCollection so maybe inspection is not accurate enough.

|jsObj|
jsObj := #{ 'x'->23. 'y'->42 }.
jsObj:=HashedCollection new.
jsObj at:'x' put:23.
jsObj at:'y' put:42;yourself.



2013/2/14 Tom <t...@tcltalk.ath.cx>
For more options, visit https://groups.google.com/groups/opt_out.


Herby Vojčík

unread,
Feb 14, 2013, 10:49:10 AM2/14/13
to amber...@googlegroups.com


Apostolis Xekoukoulotakis wrote:
> Ok. Inspecting on both objects though gives a HashedCollection so maybe
> inspection is not accurate enough.
??? Can you elaborate more. The code you entered below creates
HashedCollection in first assigment (but at once), and new
HashedCollection (but stepwise) in the other three. None of it is plain
JS Object.

Apostolis Xekoukoulotakis

unread,
Feb 14, 2013, 10:59:50 AM2/14/13
to amber...@googlegroups.com
I thought the first one was a jsObject. I don't understand the amber internals very well at this point. Using a hashedCollection has worked for me too, so I dont know when a jsObject might be required.

2013/2/14 Herby Vojčík <he...@mailbox.sk>
--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to amber-lang+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.


Herby Vojčík

unread,
Feb 14, 2013, 11:08:01 AM2/14/13
to amber...@googlegroups.com


Apostolis Xekoukoulotakis wrote:
> I thought the first one was a jsObject. I don't understand the amber
> internals very well at this point. Using a hashedCollection has worked
> for me too, so I dont know when a jsObject might be required.

#{ 'xx'->yy. 'zz'->qq } is syntax for shortcut creation of HashedCollection.

Sebastian Heidbrink

unread,
Aug 22, 2013, 5:26:14 PM8/22/13
to amber...@googlegroups.com
Hi,

 I just upgrade some older amber stuff of mine to the most current version.

I do have a problem with inlining and I do not know how to reimplement it in the current version.


    | data |
   <data = {}>.
  data at: 'x' put: ('.x input') asJQuery val.
  data at: 'y' put: ('.y input') asJQuery val.
  data at: 'z' put: ('.z input') asJQuery val.

 in the old version I could use my data object stuff like:

 data x printString

No matter how I change my old code I do always get an error "nil does not understand #at:put:" or:
" ""someother class instance like HashedCollection"" does not understand #x"

How am I supposed to reimplement such thing for the new Amber version?

Thanks
Sebastian

Manfred Kröhnert

unread,
Aug 22, 2013, 5:30:22 PM8/22/13
to amber...@googlegroups.com

Hello Sebastian,

In the latest versions of Amber it is not possible to put inline js mixed with Smalltalk code in a single method.

What prevents you from using a collection in this case?

Best,
Manfred

László Tordai

unread,
Aug 22, 2013, 5:47:12 PM8/22/13
to amber...@googlegroups.com
data := #{}.
Should do.

From: Manfred Kröhnert
Sent: 22/08/2013 23:30
To: amber...@googlegroups.com
Subject: Re: [amber-lang] Re: Initializing a new JavaScript object
[The entire original message is not included.]

Sebastian Heidbrink

unread,
Aug 22, 2013, 5:58:02 PM8/22/13
to amber...@googlegroups.com
Well, the problem is that when I do so, I get a
"a HashedCollection ('x' -> '1' , 'y' -> '2' , 'z' -> '3') does not understand x"
Error

data := #{}. was my first idea, too....

Sebastian

László Tordai

unread,
Aug 22, 2013, 6:05:54 PM8/22/13
to amber...@googlegroups.com
I think you need to use dot as separated instead of comma. Comma is concatenation.

From: Sebastian Heidbrink
Sent: 22/08/2013 23:58

Sebastian Heidbrink

unread,
Aug 22, 2013, 6:13:37 PM8/22/13
to amber...@googlegroups.com
Oh,... I wrote that misleading.

That is the message I get in the debugger doing this:

| data |
   data := #{}.

  data at: 'x' put: ('.x input') asJQuery val.
  data at: 'y' put: ('.y input') asJQuery val.
  data at: 'z' put: ('.z input') asJQuery val.

   data x printString

 with the old version and the "native object" <data = {}>. it worked very well.
 I do not really want to reimplement all the code that was implemented like that...

I guess JSProxyObject is not an option right?

Sebastian

Sebastian Heidbrink

unread,
Aug 22, 2013, 6:43:19 PM8/22/13
to amber...@googlegroups.com
:-D

What about

    data := NativeFunction contructor: 'Object'.

Seems like that works...

Sebastian

Matthias Springer

unread,
Aug 22, 2013, 7:43:24 PM8/22/13
to amber...@googlegroups.com
Hi,

just a short clarification. We are dealing with two types of dictionaries/objects here. 

Smalltalk/Amber dictionaries are instances of "HashedCollection" or "Dictionary". "#{...}" creates a Smalltalk Dictionary. You can access the value for a key with "dictionary at: key". "dictionary key" does not work, because there is no method key.

In JavaScript, every object can be seen as a dictionary. Every time we use a JavaScript object, Amber automatically wraps it inside a JSObjectProxy instance. If you send a method to the proxy that does not exist as a Smalltalk message of the proxy (e.g. "key"), the proxy forwards the message to the JavaScript object. It calls a method of the JavaScript object, if it exists, or returns a property with that name. That's why we can send "key" to a JavaScript dictionary. Your old code "<data = {};> created a JavaScript object and JSObjectProxy supports working with the object with "at:" and sending messages to it directly.

You should use HashedCollection objects if you're passing the objects to external JavaScript libraries, since Dictionary has a different internal layout.

In your case, you could create a subclass of HashedCollection/Dictionary and add this method (treat all unknown messages as dictionary lookup):

doesNotUnderstand: aMessage
^ self at: aMessage selector

Then create instances of your new subclass instead of "#{}". Or create a plain JavaScript object (and its proxy) with "NativeFunction".

Matthias

Nicolas Petton

unread,
Aug 23, 2013, 8:37:07 AM8/23/13
to amber...@googlegroups.com
If you really need an empty JS object from Amber, I would put that into a separate method:

emptyData
<return {};>

then use it as you did in the past.

HTH,
nico

Sebastian Heidbrink

unread,
Aug 23, 2013, 9:57:45 AM8/23/13
to amber...@googlegroups.com
Am 23.08.2013 05:37, schrieb Nicolas Petton:
<return {};>

Thank you Nicholas!

I think, I tried <return {}> and that didn't work,....

But this worked,....

I need to keep those semi colons in my mind....

And @Matthias thank you for the detailed explanation. It in fact an interesting idea to subclass HashedCollection....

Sebastian

Herby Vojčík

unread,
Aug 23, 2013, 10:21:30 AM8/23/13
to amber...@googlegroups.com


Nicolas Petton wrote:
> If you really need an empty JS object from Amber, I would put that into
> a separate method:
>
> emptyData
> <return {};>

I use JSON parse: '{}'.

Herby

tgkuo

unread,
Aug 23, 2013, 10:28:45 AM8/23/13
to amber...@googlegroups.com

What about a function object that was contracted by new Function?

is the JS object created this way also applied to the function object?


Tsun

Herby Vojčík

unread,
Aug 23, 2013, 10:20:18 AM8/23/13
to amber...@googlegroups.com


Matthias Springer wrote:
> Hi,
>
> just a short clarification. We are dealing with two types of
> dictionaries/objects here.
>
> Smalltalk/Amber dictionaries are instances of "HashedCollection" or
> "Dictionary". "#{...}" creates a Smalltalk Dictionary. You can access

#{...} creates HashedCollection. not dictionary; therefore it is ok to
pass it to JS code. Dictionary would not work.

Herby
Reply all
Reply to author
Forward
0 new messages