Define properties in a constructor?

49 views
Skip to first unread message

Michael McGlothlin

unread,
Oct 14, 2015, 12:37:03 PM10/14/15
to streng...@googlegroups.com
Is there a way in strong mode to define properties (from an array or key and default value pairs for example) in a constructor since it doesn't allow you to use 'this'?

Andreas Rossberg

unread,
Oct 14, 2015, 12:46:40 PM10/14/15
to Michael McGlothlin, Strengthen JS
On 14 October 2015 at 18:37, Michael McGlothlin
We will probably lift the restriction on 'this' somewhat, which might
enable that. But that's going to be more by chance than by design,
that kind of reflection isn't something we particularly aim to
support. Especially because it's going to be impossible to type-check.
In fact, I'm hoping that, with the ongoing ES proposal for field
declarations, the usual pattern for defining properties is going to be
fully declarative.

/Andreas

Michael McGlothlin

unread,
Oct 14, 2015, 2:56:55 PM10/14/15
to Andreas Rossberg, Strengthen JS
Overall I agree with you but every rule has exceptions (or at least times when I can't think of a better way to do it).

In this case I have got tired of trying to handle complex arguments with boiler plate code all over the place. I wanted a rule-based system that'd define what was expected and process things according to those rules. So it defines properties with getters and setters that behave according to a few simple rules instead of a bunch of almost identical code.

Ended up doing something like Object.defineProperty( ()=>this, name, {get: .., set: ..}) which seems to work.

Michael McGlothlin

unread,
Oct 14, 2015, 3:13:21 PM10/14/15
to Andreas Rossberg, Strengthen JS
Actually ()=>this is (()=>this)() .. in case anyone else needs to do it.

Andreas Rossberg

unread,
Oct 14, 2015, 4:19:37 PM10/14/15
to Michael McGlothlin, Strengthen JS
Oh! That actually is a bug, at least as far as the intended behaviour
of what's currently implemented goes. (Arrow functions landed after
the `this` restriction for strong classes, so this was overlooked.)

But yeah, we will probably lift the `this` restriction, so your code
will likely continue to work. Just be aware that it will defeat all
potential invariants and performance improvements you might otherwise
get for that class.

/Andreas


On 14 October 2015 at 21:13, Michael McGlothlin

Michael McGlothlin

unread,
Oct 14, 2015, 5:41:42 PM10/14/15
to Andreas Rossberg, Strengthen JS
I guess what I'm trying to achieve is something of a metaclass (dynamically adding getters/setters from the constructor). Would using Proxy() to achieve something similar be any better so far as performance? 

Andreas Rossberg

unread,
Oct 15, 2015, 2:01:15 AM10/15/15
to Michael McGlothlin, Strengthen JS
On 14 October 2015 at 23:41, Michael McGlothlin
<mike.mc...@gmail.com> wrote:
> I guess what I'm trying to achieve is something of a metaclass (dynamically
> adding getters/setters from the constructor). Would using Proxy() to achieve
> something similar be any better so far as performance?

Using proxies is probably the worst possibility by far, in all
dimensions. Avoid them if you can.

So, I'm afraid the only thing I can say is: meta-classes or reflection
of the kind you suggest are not something strong mode is designed to
support. And I wouldn't know how to support it either, it's
fundamentally incompatible with the design goals. Fortunately, where
you need something like that, fine-grained interop with weak code is
possible, and that is by design. :)

/Andreas
Message has been deleted

Youness Belfkih

unread,
Mar 26, 2016, 2:47:42 AM3/26/16
to Strengthen JS, ross...@google.com
maybe you should wait for ES7 decorators currently implmented by TypeScript

Andreas Rossberg

unread,
Apr 4, 2016, 11:33:11 AM4/4/16
to Youness Belfkih, Strengthen JS
FWIW, ES7 is pretty much done; it does not have decorators. Also, decorators wouldn't help at all, since they are neither declarative nor do they even have a stable meaning.
Reply all
Reply to author
Forward
0 new messages