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.

Herby

Axel Rauschmayer wrote:
[Referring to Allen’s slides:
http://wiki.ecmascript.org/lib/exe/fetch.php?id=meetings%3Ameeting_jan_29_2013&cache=cache&media=meetings:subclassing_builtins.pdf
<http://wiki.ecmascript.org/lib/exe/fetch.php?id=meetings%3Ameeting_jan_29_2013&cache=cache&media=meetings:subclassing_builtins.pdf>
]

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):
super.constructor(...)

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?

Thanks!

Axel

--
Dr. Axel Rauschmayer
a...@rauschma.de <mailto:a...@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@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to