Constructors need to be able to recognize uninitalized instances(?)

Axel Rauschmayer axel at rauschma.de
Sun Mar 17 09:20:44 PDT 2013


[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 ]

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
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130317/4db910c3/attachment-0001.html>


More information about the es-discuss mailing list