How do I call a JavaScript constructor // Proper mapping of JS object hierarchies

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

How do I call a JavaScript constructor // Proper mapping of JS object hierarchies

Hannes Hirzel
Hi
A question related to this other thread with morphic.js  (subj:
'Delegation, anybody)

The JavaScript library
morphic.js  has a class hierarchy implemented with constructor
functions and prototype extensions.

For example BoxMorph is a subclass of Morph.

How should I construct new objects in Amber?
For example a new Point, or a new Color instance?

Morphic js code: http://chirp.scratchr.org/dl/experimental/JsMorphic/morphic.js
Morphic js documentation:
http://chirp.scratchr.org/dl/experimental/JsMorphic/morphic.txt
(NEW 20th Feb 2012)

Regarding the mapping of the JavaScript object hierarchy: I assume
there is no need to do that once one has figured out how to construct
the objects.

Kind regards
Hannes
Reply | Threaded
Open this post in threaded view
|

Re: How do I call a JavaScript constructor // Proper mapping of JS object hierarchies

Ankh'nAton
On 23 Feb., 02:06, "H. Hirzel" <[hidden email]> wrote:

> Hi
> A question related to this other thread with morphic.js  (subj:
> 'Delegation, anybody)
>
> The JavaScript library
> morphic.js  has a class hierarchy implemented with constructor
> functions and prototype extensions.
>
> For example BoxMorph is a subclass of Morph.
>
> How should I construct new objects in Amber?

Just go like...

  m := Morph new.

and then initialize that morph like...

  m setColor: 'rgba(80,80,160,1)'.

Both Amber and Morphic define a Point class which cannot be
interchanged directly. Therefor I added a method #asMorphicPoint to
Amber's Point class in the Kernel-Objects category.

  asMorphicPoint
    < return Point(this['@x],this['@y']); >

Then you can write..

  m setPosition: (100@80) asMorphicPoint.

..or..

  m moveBy: (20@20) asMorphicPoint.


Regards...
Reply | Threaded
Open this post in threaded view
|

Re: How do I call a JavaScript constructor // Proper mapping of JS object hierarchies

Nicolas Petton
On 23/02/12 10:32, Tom wrote:

> Both Amber and Morphic define a Point class which cannot be
> interchanged directly. Therefor I added a method #asMorphicPoint to
> Amber's Point class in the Kernel-Objects category.
>
>    asMorphicPoint
>      <  return Point(this['@x],this['@y']);>
>
> Then you can write..
>
>    m setPosition: (100@80) asMorphicPoint.


You can probably use the window object as a namespace here:

window Point new....

Nico

--
Nicolas Petton
http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: How do I call a JavaScript constructor // Proper mapping of JS object hierarchies

Hannes Hirzel
Yes, Nicolas

this works
"==================================="
p := window Point new.
p x: 20; y:10.
p inspect
"==================================="

whereas this does not
"-----------------------------------------------------------------------"
p := (window Point new) x: 20; y:10.
p inspect
"-----------------------------------------------------------------------"

For Color the constructor function in
http://chirp.scratchr.org/dl/experimental/JsMorphic/morphic.js

is
// Color instance creation:

function Color(r, g, b, a) {
        // all values are optional, just (r, g, b) is fine
        this.r = r || 0;
        this.g = g || 0;
        this.b = b || 0;
        this.a = a || ((a === 0) ? 0 : 1);
}

The question is how is this to be called from Amber?

--Hannes

On 2/23/12, Nicolas Petton <[hidden email]> wrote:

> On 23/02/12 10:32, Tom wrote:
>> Both Amber and Morphic define a Point class which cannot be
>> interchanged directly. Therefor I added a method #asMorphicPoint to
>> Amber's Point class in the Kernel-Objects category.
>>
>>    asMorphicPoint
>>      <  return Point(this['@x],this['@y']);>
>>
>> Then you can write..
>>
>>    m setPosition: (100@80) asMorphicPoint.
>
>
> You can probably use the window object as a namespace here:
>
> window Point new....
>
> Nico
>
> --
> Nicolas Petton
> http://nicolas-petton.fr
>
Reply | Threaded
Open this post in threaded view
|

Re: How do I call a JavaScript constructor // Proper mapping of JS object hierarchies

Nicolas Petton
On 23/02/12 11:15, H. Hirzel wrote:

> Yes, Nicolas
>
> this works
> "==================================="
> p := window Point new.
> p x: 20; y:10.
> p inspect
> "==================================="
>
> whereas this does not
> "-----------------------------------------------------------------------"
> p := (window Point new) x: 20; y:10.
> p inspect
> "-----------------------------------------------------------------------"
>

My guess is that's because you miss #yourself here. Try:

p := window Point new
        x: 20;
        y:10;
        yourself

> For Color the constructor function in
> http://chirp.scratchr.org/dl/experimental/JsMorphic/morphic.js
>
> is
> // Color instance creation:
>
> function Color(r, g, b, a) {
> // all values are optional, just (r, g, b) is fine
> this.r = r || 0;
> this.g = g || 0;
> this.b = b || 0;
> this.a = a || ((a === 0) ? 0 : 1);
> }
>
> The question is how is this to be called from Amber?

the simplest is to do:

color := Color new
        r: 30;
        g: 10;
        b: 2;
        a: 1;
        yourself

Cheers,
Nico

>
> --Hannes
>
> On 2/23/12, Nicolas Petton<[hidden email]>  wrote:
>> On 23/02/12 10:32, Tom wrote:
>>> Both Amber and Morphic define a Point class which cannot be
>>> interchanged directly. Therefor I added a method #asMorphicPoint to
>>> Amber's Point class in the Kernel-Objects category.
>>>
>>>     asMorphicPoint
>>>       <   return Point(this['@x],this['@y']);>
>>>
>>> Then you can write..
>>>
>>>     m setPosition: (100@80) asMorphicPoint.
>>
>>
>> You can probably use the window object as a namespace here:
>>
>> window Point new....
>>
>> Nico
>>
>> --
>> Nicolas Petton
>> http://nicolas-petton.fr
>>


--
Nicolas Petton
http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: How do I call a JavaScript constructor // Proper mapping of JS object hierarchies

Hannes Hirzel
On 2/23/12, Nicolas Petton <[hidden email]> wrote:

> On 23/02/12 11:15, H. Hirzel wrote:
>> Yes, Nicolas
>>
>> this works
>> "==================================="
>> p := window Point new.
>> p x: 20; y:10.
>> p inspect
>> "==================================="
>>
>> whereas this does not
>> "-----------------------------------------------------------------------"
>> p := (window Point new) x: 20; y:10.
>> p inspect
>> "-----------------------------------------------------------------------"
>>
>
> My guess is that's because you miss #yourself here. Try:
>
> p := window Point new
> x: 20;
> y:10;
> yourself

Yes, this is fine.


>> For Color the constructor function in
>> http://chirp.scratchr.org/dl/experimental/JsMorphic/morphic.js
>>
>> is
>> // Color instance creation:
>>
>> function Color(r, g, b, a) {
>> // all values are optional, just (r, g, b) is fine
>> this.r = r || 0;
>> this.g = g || 0;
>> this.b = b || 0;
>> this.a = a || ((a === 0) ? 0 : 1);
>> }
>>
>> The question is how is this to be called from Amber?
>
> the simplest is to do:
>
> color := Color new
> r: 30;
> g: 10;
> b: 2;
> a: 1;
> yourself

This is fine as well. Thank you!
I'll now will look into Tom's wrapper (AMorph).

--Hannes

> Cheers,
> Nico
>
>>
>> --Hannes
>>
>> On 2/23/12, Nicolas Petton<[hidden email]>  wrote:
>>> On 23/02/12 10:32, Tom wrote:
>>>> Both Amber and Morphic define a Point class which cannot be
>>>> interchanged directly. Therefor I added a method #asMorphicPoint to
>>>> Amber's Point class in the Kernel-Objects category.
>>>>
>>>>     asMorphicPoint
>>>>       <   return Point(this['@x],this['@y']);>
>>>>
>>>> Then you can write..
>>>>
>>>>     m setPosition: (100@80) asMorphicPoint.
>>>
>>>
>>> You can probably use the window object as a namespace here:
>>>
>>> window Point new....
>>>
>>> Nico
>>>
>>> --
>>> Nicolas Petton
>>> http://nicolas-petton.fr
>>>
>
>
> --
> Nicolas Petton
> http://nicolas-petton.fr
>
Reply | Threaded
Open this post in threaded view
|

Re: How do I call a JavaScript constructor // Proper mapping of JS object hierarchies

Ankh'nAton
In reply to this post by Hannes Hirzel
For the color problem I have written a wrapper class to be used
like...

  myMorph setColor: (AColor new: #(80 80 160)) asColor.

...to stay completely within a Smalltalk hierarchy of classes.

I will release a new version of my morphicAmber.zip in a few days
which will contain most of my new wrapper classes for Morphic and
rebuilds my demonstrational startup screen using these wrapper
classes.

Cheers...

On 23 Feb., 11:15, "H. Hirzel" <[hidden email]> wrote:

> Yes, Nicolas
>
> this works
> "==================================="
> p := window Point new.
> p x: 20; y:10.
> p inspect
> "==================================="
>
> whereas this does not
> "-----------------------------------------------------------------------"
> p := (window Point new) x: 20; y:10.
> p inspect
> "-----------------------------------------------------------------------"
>
> For Color the constructor function inhttp://chirp.scratchr.org/dl/experimental/JsMorphic/morphic.js
>
> is
> // Color instance creation:
>
> function Color(r, g, b, a) {
>         // all values are optional, just (r, g, b) is fine
>         this.r = r || 0;
>         this.g = g || 0;
>         this.b = b || 0;
>         this.a = a || ((a === 0) ? 0 : 1);
>
> }
>
> The question is how is this to be called from Amber?
>
> --Hannes
>
> On 2/23/12, Nicolas Petton <[hidden email]> wrote:
>
>
>
>
>
>
>
> > On 23/02/12 10:32, Tom wrote:
> >> Both Amber and Morphic define a Point class which cannot be
> >> interchanged directly. Therefor I added a method #asMorphicPoint to
> >> Amber's Point class in the Kernel-Objects category.
>
> >>    asMorphicPoint
> >>      <  return Point(this['@x],this['@y']);>
>
> >> Then you can write..
>
> >>    m setPosition: (100@80) asMorphicPoint.
>
> > You can probably use the window object as a namespace here:
>
> > window Point new....
>
> > Nico
>
> > --
> > Nicolas Petton
> >http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: How do I call a JavaScript constructor // Proper mapping of JS object hierarchies

Ankh'nAton
Ha!

Why not put #asColor into the Array class? :)

  #(80 80 120) asColor

...is far better and makes the wrapper class AColor unnecessary.

Regards...

On 23 Feb., 12:23, Tom <[hidden email]> wrote:

> For the color problem I have written a wrapper class to be used
> like...
>
>   myMorph setColor: (AColor new: #(80 80 160)) asColor.
>
> ...to stay completely within a Smalltalk hierarchy of classes.
>
> I will release a new version of my morphicAmber.zip in a few days
> which will contain most of my new wrapper classes for Morphic and
> rebuilds my demonstrational startup screen using these wrapper
> classes.
>
> Cheers...
>
> On 23 Feb., 11:15, "H. Hirzel" <[hidden email]> wrote:
>
>
>
>
>
>
>
> > Yes, Nicolas
>
> > this works
> > "==================================="
> > p := window Point new.
> > p x: 20; y:10.
> > p inspect
> > "==================================="
>
> > whereas this does not
> > "-----------------------------------------------------------------------"
> > p := (window Point new) x: 20; y:10.
> > p inspect
> > "-----------------------------------------------------------------------"
>
> > For Color the constructor function inhttp://chirp.scratchr.org/dl/experimental/JsMorphic/morphic.js
>
> > is
> > // Color instance creation:
>
> > function Color(r, g, b, a) {
> >         // all values are optional, just (r, g, b) is fine
> >         this.r = r || 0;
> >         this.g = g || 0;
> >         this.b = b || 0;
> >         this.a = a || ((a === 0) ? 0 : 1);
>
> > }
>
> > The question is how is this to be called from Amber?
>
> > --Hannes
>
> > On 2/23/12, Nicolas Petton <[hidden email]> wrote:
>
> > > On 23/02/12 10:32, Tom wrote:
> > >> Both Amber and Morphic define a Point class which cannot be
> > >> interchanged directly. Therefor I added a method #asMorphicPoint to
> > >> Amber's Point class in the Kernel-Objects category.
>
> > >>    asMorphicPoint
> > >>      <  return Point(this['@x],this['@y']);>
>
> > >> Then you can write..
>
> > >>    m setPosition: (100@80) asMorphicPoint.
>
> > > You can probably use the window object as a namespace here:
>
> > > window Point new....
>
> > > Nico
>
> > > --
> > > Nicolas Petton
> > >http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: How do I call a JavaScript constructor // Proper mapping of JS object hierarchies

Ankh'nAton
Hmm...,

I tried a new method for the Array class:

asColor
  | clr |
  clr := Color new.
  (self size = 3)
    ifTrue: [
      clr r: (self at: 1); g: (self at: 2); b: (self at: 3).
    ] ifFalse: [
      clr r: 0; g: 0; b: 0.
    ].
  ^clr


It works.

Regards...

On 23 Feb., 12:36, Tom <[hidden email]> wrote:

> Ha!
>
> Why not put #asColor into the Array class? :)
>
>   #(80 80 120) asColor
>
> ...is far better and makes the wrapper class AColor unnecessary.
>
> Regards...
>
> On 23 Feb., 12:23, Tom <[hidden email]> wrote:
>
>
>
>
>
>
>
> > For the color problem I have written a wrapper class to be used
> > like...
>
> >   myMorph setColor: (AColor new: #(80 80 160)) asColor.
>
> > ...to stay completely within a Smalltalk hierarchy of classes.
>
> > I will release a new version of my morphicAmber.zip in a few days
> > which will contain most of my new wrapper classes for Morphic and
> > rebuilds my demonstrational startup screen using these wrapper
> > classes.
>
> > Cheers...
>
> > On 23 Feb., 11:15, "H. Hirzel" <[hidden email]> wrote:
>
> > > Yes, Nicolas
>
> > > this works
> > > "==================================="
> > > p := window Point new.
> > > p x: 20; y:10.
> > > p inspect
> > > "==================================="
>
> > > whereas this does not
> > > "-----------------------------------------------------------------------"
> > > p := (window Point new) x: 20; y:10.
> > > p inspect
> > > "-----------------------------------------------------------------------"
>
> > > For Color the constructor function inhttp://chirp.scratchr.org/dl/experimental/JsMorphic/morphic.js
>
> > > is
> > > // Color instance creation:
>
> > > function Color(r, g, b, a) {
> > >         // all values are optional, just (r, g, b) is fine
> > >         this.r = r || 0;
> > >         this.g = g || 0;
> > >         this.b = b || 0;
> > >         this.a = a || ((a === 0) ? 0 : 1);
>
> > > }
>
> > > The question is how is this to be called from Amber?
>
> > > --Hannes
>
> > > On 2/23/12, Nicolas Petton <[hidden email]> wrote:
>
> > > > On 23/02/12 10:32, Tom wrote:
> > > >> Both Amber and Morphic define a Point class which cannot be
> > > >> interchanged directly. Therefor I added a method #asMorphicPoint to
> > > >> Amber's Point class in the Kernel-Objects category.
>
> > > >>    asMorphicPoint
> > > >>      <  return Point(this['@x],this['@y']);>
>
> > > >> Then you can write..
>
> > > >>    m setPosition: (100@80) asMorphicPoint.
>
> > > > You can probably use the window object as a namespace here:
>
> > > > window Point new....
>
> > > > Nico
>
> > > > --
> > > > Nicolas Petton
> > > >http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: How do I call a JavaScript constructor // Proper mapping of JS object hierarchies

Ankh'nAton
An here's the final version for anArray asColor

asColor
  "tb: Return a color by interpretating the first three elements of
the receiver as r,g,b of a color"
  | clr |
  clr := Color new.
  (self size >= 3) ifTrue: [
    clr r: (self at: 1); g: (self at: 2); b: (self at: 3).
  ].
  ^clr

It's pretty forward, tolerates arrays with more than three elements
and finally always returns a color.

Tom
Reply | Threaded
Open this post in threaded view
|

Re: How do I call a JavaScript constructor // Proper mapping of JS object hierarchies

Hannes Hirzel
Yes, this is fine. But there is a forth possible parameter, Morph
transparency (alpha value).

morphic.js
( http://chirp.scratchr.org/dl/experimental/JsMorphic/morphic.js )

// Color instance creation:

function Color(r, g, b, a) {
        // all values are optional, just (r, g, b) is fine
        this.r = r || 0;
        this.g = g || 0;
        this.b = b || 0;
        this.a = a || ((a === 0) ? 0 : 1);
}

--Hannes

On 2/23/12, Tom <[hidden email]> wrote:

> An here's the final version for anArray asColor
>
> asColor
>   "tb: Return a color by interpretating the first three elements of
> the receiver as r,g,b of a color"
>   | clr |
>   clr := Color new.
>   (self size >= 3) ifTrue: [
>     clr r: (self at: 1); g: (self at: 2); b: (self at: 3).
>   ].
>   ^clr
>
> It's pretty forward, tolerates arrays with more than three elements
> and finally always returns a color.
>
> Tom
>
Reply | Threaded
Open this post in threaded view
|

Re: How do I call a JavaScript constructor // Proper mapping of JS object hierarchies

Ankh'nAton
Yes, I left the fourth parameter out for simplicity. Though, You can
always add transparency later and go like...

  | clr |
  clr := (#(40 40 80) asColor) a: 0.5; yourself.

Cheers...

On 23 Feb., 14:37, "H. Hirzel" <[hidden email]> wrote:

> Yes, this is fine. But there is a forth possible parameter, Morph
> transparency (alpha value).
>
> morphic.js
> (http://chirp.scratchr.org/dl/experimental/JsMorphic/morphic.js)
>
> // Color instance creation:
>
> function Color(r, g, b, a) {
>         // all values are optional, just (r, g, b) is fine
>         this.r = r || 0;
>         this.g = g || 0;
>         this.b = b || 0;
>         this.a = a || ((a === 0) ? 0 : 1);
>
> }
>
> --Hannes
>
> On 2/23/12, Tom <[hidden email]> wrote:
>
>
>
>
>
>
>
> > An here's the final version for anArray asColor
>
> > asColor
> >   "tb: Return a color by interpretating the first three elements of
> > the receiver as r,g,b of a color"
> >   | clr |
> >   clr := Color new.
> >   (self size >= 3) ifTrue: [
> >     clr r: (self at: 1); g: (self at: 2); b: (self at: 3).
> >   ].
> >   ^clr
>
> > It's pretty forward, tolerates arrays with more than three elements
> > and finally always returns a color.
>
> > Tom