Syntax Proposal: Allow Java-like Object Literals after constructor calls to set properties on created objects.

Erik Arvidsson erik.arvidsson at gmail.com
Wed Jun 30 14:15:52 PDT 2010


Sorry, Object.create was a mistake. It is only useful for low level code and
not for every day usage. Also, to use Object.create you need the following
which is even worse for an every day usage.

var stopButton = Object.create(ImageButton.prototype, {
  image: {
    value: getImage('stop.png'),
    writable: true,
    configurable: true,
    enumerable: true
  },
  size: {
    value: buttonSize,
    writable: true,
    configurable: true,
    enumerable: true
  },
  toolTip: {
    value: 'Stop Running Scripts',
    writable: true,
    configurable: true,
    enumerable: true
  },
  onClick: {
    value:stopAll,
    writable: true,
    configurable: true,
    enumerable: true
  }
});

Even with all this boilerplate code it does not cover the code that happens
inside the constructor so the code would get even longer.

var stopButton = new ImageButton(this);
Object.defineProperties(stopButton, {
  image: {
    value: getImage('stop.png'),
    writable: true,
    configurable: true,
    enumerable: true
  },
  size: {
    value: buttonSize,
    writable: true,
    configurable: true,
    enumerable: true
  },
  toolTip: {
    value: 'Stop Running Scripts',
    writable: true,
    configurable: true,
    enumerable: true
  },
  onClick: {
    value: stopAll,
    writable: true,
    configurable: true,
    enumerable: true
  }
});

erik

On Wed, Jun 30, 2010 at 13:52, Allen Wirfs-Brock <
Allen.Wirfs-Brock at microsoft.com> wrote:

> Not quite as concise but:
>
> var stopButton = Object.create(ImageButton.prototype, {
>                image: {value: getImage('stop.png')},
>                size: {value: buttonSize},
>                toolTip: {value: 'Stop Running Scripts'},
>                onClick: {value:stopAll}
>        });
>
> Of course, if you wanted something other than the default attribute values
> you would have to also add those.
>
> Allen
>
> > -----Original Message-----
> > From: es-discuss-bounces at mozilla.org [mailto:es-discuss-
> > bounces at mozilla.org] On Behalf Of Jeff Watkins
> > Sent: Wednesday, June 30, 2010 10:26 AM
> > To: Jürg Lehni
> > Cc: es-discuss
> > Subject: Re: Syntax Proposal: Allow Java-like Object Literals after
> constructor
> > calls to set properties on created objects.
> >
> > Just out of curiosity, what's wrong with the idiomatic Javascript way of
> passing
> > an object literal as your last constructor argument? So your example
> becomes:
> >
> >       var stopButton = new ImageButton(this, {
> >               image: getImage('stop.png'),
> >               size: buttonSize,
> >               toolTip: 'Stop Running Scripts',
> >               onClick: stopAll
> >       });
> >
> > Granted, you wind up with an extra comma...
> >
> > On 30 Jun, 2010, at 10:05 AM, Jürg Lehni wrote:
> >
> > > I am still interested in hearing more feedback on this. Maybe my
> examples
> > were not so clear?
> > >
> > > As more real world example, taken from a UI library that I am working
> with,
> > would look like this:
> > >
> > >     var stopButton = new ImageButton(this) {
> > >             image: getImage('stop.png'),
> > >             size: buttonSize,
> > >             toolTip: 'Stop Running Scripts',
> > >             onClick: stopAll
> > >     };
> > >
> > >
> > > Again, all the properties from the object literal immediately following
> the
> > constructor call would then be set on the created object.
> > >
> > > Rhino allows me to use this already and it has been proven to be very
> useful in
> > many occasions, leading to cleaner and more readable code.
> > >
> > > Jürg
> > >
> > > On 8 Jun 2010, at 20:57, Mike Samuel wrote:
> > >
> > >> A lot of people put opening semicolons on a new line, including the
> > >> Rhino authors.
> > >> How would semicolon insertion in this proposal interact with that
> > >> formatting convention?
> > >>   var runnable = new java.lang.Runnable()
> > >>   {
> > >>     run: function ()
> > >>     {
> > >>     }
> > >>   };
> > >>
> > >>
> > >> 2010/6/8 Jürg Lehni <lists at scratchdisk.com>:
> > >>> This simple proposal is inspired by an extension of Rhino that
> currently
> > allows to implement its syntax for anonymous Java interface
> implementation.
> > Here an example that creates an anonymous class implementing the Runnable
> > interface and defining the run method in an anonymous object literal that
> > (mimicking a Java code block) immediately following the constructor call:
> > >>>
> > >>> var runnable = new java.lang.Runnable() {
> > >>>       run: function() {
> > >>>       }
> > >>> };
> > >>>
> > >>> When looking deeper into how Rhino achieves this syntax, I found out
> that it
> > simply appends the following anonymous object literal to the list of
> arguments
> > of whatever constructor came before. So the following code works in Rhino
> and
> > prints the content of the hello string to the console:
> > >>>
> > >>> function Test(obj) {
> > >>>       print(obj.hello);
> > >>> }
> > >>>
> > >>> new Test() {
> > >>>       hello: 'Greetings, I am an anonymous object literal'
> > >>> };
> > >>>
> > >>> For the Illustrator scripting plugin http://scriptographer.org I
> came up with
> > the convention to (ab)use this non-standard feature to allow setting of
> > properties on freshly created objects, by extending the underlying Java
> proxy
> > objects to automatically detect such a passed object literal, iterate
> through its
> > properties and set them on the newly created object (In Scriptographer it
> is then
> > also removed from the argument list). Soon it became apparent that this
> is very
> > useful and also leads to cleaner code. I therefore started to wonder if
> this would
> > make sense as an syntax extension in ES5. Here another example.
> > >>>
> > >>> function MyConstructor(param) {
> > >>>       print(param); // Should not print the object literal }
> > >>>
> > >>> var obj = new MyConstructor() {
> > >>>       property: 'This will be automatically set on the created
> object'
> > >>> };
> > >>>
> > >>> print(obj.property); // 'This will...created object'
> > >>>
> > >>> So far I cannot see any syntax conflicts.
> > >>>
> > >>> I am wondering what you all think of this proposal and look forward
> to your
> > thoughts.
> > >>>
> > >>> Jürg
> > >>> _______________________________________________
> > >>> es-discuss mailing list
> > >>> es-discuss at mozilla.org
> > >>> https://mail.mozilla.org/listinfo/es-discuss
> > >>>
> > >
> > > _______________________________________________
> > > es-discuss mailing list
> > > es-discuss at mozilla.org
> > > https://mail.mozilla.org/listinfo/es-discuss
> >
> > _______________________________________________
> > es-discuss mailing list
> > es-discuss at mozilla.org
> > https://mail.mozilla.org/listinfo/es-discuss
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>



-- 
erik
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20100630/21125eba/attachment-0001.html>


More information about the es-discuss mailing list