Constructors need to be able to recognize uninitalized instances(?)
herby at mailbox.sk
Mon Mar 18 04:23:14 PDT 2013
I think the idea of detecting if you have initialized or noninitialized
instance at runtime is very unhappy idea.
You know when it is called as an initializer (2, 4 below) and when not
(1 below). 3 below is legacy way of doing super - it can be simply replaced.
And there is also no breaking compatibility cost - so far, there was no
API to distinguish this; so once it is added, it can be safely specified
that "initializer" context is only 2 and 4.
Axel Rauschmayer wrote:
> [Referring to Allen’s slides:
> Is this really true?
> I can see four ways of invoking a constructor function C:
> 1. As a function: C(...)
> 2. Via `new`: new C(...)
> 3. Via `call`: C.call(this, ...)
> 4. Via `super`, in a sub-instance, as a method (similar to #3):
> C[@@create] is only invoked during #2 (by the `new` operator). Thus, the
> constructor’s role is always: set up an uninitialized instance. You
> could add a check against an instance being initialized twice, but that
> doesn’t seem to be what the slides are about.
> The slides mention one use case: What if you want to have a function Foo
> that can be either called as a function or as a constructor? You can’t
> detect the difference if Foo is inside a namespace object. That is, you
> want to distinguish #1 from all others, but #1 could happen to a
> namespaced C. Observations:
> – Does recognizing whether `this` is initialized really help here? Isn’t
> it more about checking whether `this` is an instance of C?
> – I’ve always considered this to be more of an anti-pattern for
> non-builtin code. Thus, it seems useful to support a check, but only so
> that namespaced constructors can avoid accidentally polluting the
> namespace object.
> – Won’t namespace objects go away with modules?
> Dr. Axel Rauschmayer
> axel at rauschma.de <mailto:axel at rauschma.de>
> home: rauschma.de <http://rauschma.de>
> twitter: twitter.com/rauschma <http://twitter.com/rauschma>
> blog: 2ality.com <http://2ality.com>
> es-discuss mailing list
> es-discuss at mozilla.org
More information about the es-discuss