From the proposal...It is a (early) ReferenceError to use ‘this’ in a class’s constructor, other than as the
object targeted in a property assignment. All such assignments have to be expressionstatements directly in the statement list of the constructor body.That sounds far too restrictive to me, some classes may need to initialize themselves by doing more than simply assigning values to properties.
... so far we don't have a good solution that wouldn't allow half-constructed objects to escape from constructors arbitrarily ...
... so far we don't have a good solution that wouldn't allow half-constructed objects to escape from constructors arbitrarily ...Would passing a method out from a class constructor after the class properties have been dealt with pose a half-constructed-object risk? I can see a potential problem occurring if a class method is invoked prior to all class properties being assigned values but once the latter has been done the class/object would be stable enough, wouldn't it?
On Friday, 13 March 2015 10:52:59 UTC, Andreas Rossberg wrote:On 12 March 2015 at 23:47, Si Robertson <retrom...@gmail.com> wrote:From the proposal...It is a (early) ReferenceError to use ‘this’ in a class’s constructor, other than as the
object targeted in a property assignment. All such assignments have to be expressionstatements directly in the statement list of the constructor body.That sounds far too restrictive to me, some classes may need to initialize themselves by doing more than simply assigning values to properties.Yes, this definitely is a serious pain point. We are fully aware that this is overly restrictive, and would like to relax it. But so far we don't have a good solution that wouldn't allow half-constructed objects to escape from constructors arbitrarily, and thereby break most invariants we'd like to establish. So to get moving, we erred on a conservative semantics for the time being. We certainly intend to revisit this point in the future (see also the short discussion in the strawman)./Andreas
--
You received this message because you are subscribed to the Google Groups "Strengthen JS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to strengthen-j...@googlegroups.com.
To post to this group, send email to streng...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/strengthen-js/5b2f775d-7277-4b42-8ea7-cc76c7ee7e1f%40googlegroups.com.
... so far we don't have a good solution that wouldn't allow half-constructed objects to escape from constructors arbitrarily ...Would passing a method out from a class constructor after the class properties have been dealt with pose a half-constructed-object risk? I can see a potential problem occurring if a class method is invoked prior to all class properties being assigned values but once the latter has been done the class/object would be stable enough, wouldn't it?
I'm still not keen on this restriction.
Over the last couple of months I have been keeping tabs on the number of times I use this within a class constructor, beyond property assignment, and it's quite a lot. The use of Object.freeze(), Object.seal(), and Object.defineProperty() within a class constructor appears to the be main reason for me passing this around. There are also a few libraries of mine that keep hold of object references until those objects are revoked, and that also involves passing this outside of the class constructor.
How many other OOP based programming languages enforce a similar restriction?
On Thursday, 12 March 2015 22:47:33 UTC, Si Robertson wrote:From the proposal...It is a (early) ReferenceError to use ‘this’ in a class’s constructor, other than as the
object targeted in a property assignment. All such assignments have to be expressionstatements directly in the statement list of the constructor body.That sounds far too restrictive to me, some classes may need to initialize themselves by doing more than simply assigning values to properties. For example...class MyAwesomeEventSynchronizer {
constructor(target) {
// property assignment goes here
target.addEventListener("mousedown", this.handleMouseDown); // error?
target.addEventListener("mouseup", this.handleMouseUp); // error?
window.requestAnimationFrame(this.flushEvents); // error?
}
// functions go here}How is this kind of situation going to be dealt with in strong mode?
--
You received this message because you are subscribed to the Google Groups "Strengthen JS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to strengthen-j...@googlegroups.com.
To post to this group, send email to streng...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/strengthen-js/bc793ea9-43e2-4a9a-92ed-581b3fd233f4%40googlegroups.com.