Determining if an object can be constructed

Bradley Meck bradley.meck at gmail.com
Fri Jan 20 23:55:39 UTC 2017


> 1. every call to `new Foo(...args)` creates a distinct object not
reference-equal to any other such call.

Would this mean

```
function factory() { return []; }
```

would qualify as a constructor?

On Fri, Jan 20, 2017 at 5:29 PM, Scott Sauyet <scott at sauyet.com> wrote:

> Bradley Meck wrote:
> > I am unsure the exact question.
>
> I certainly agree that it's not a well-formulated question.  Perhaps
> in the end it's not even coherent.  But it seems to make some sense.
> I suspect that the answer is simply that it cannot be done.  But I
> don't know how to demonstrate that.
>
>
> > `new` will allocate an object with a prototype for sure, but it is never
> > guaranteed to return that object even with classes.
> >
> > ```js
> > let a = {};
> > class Foo {
> >   constructor() {
> >     return a;
> >   }
> > }
> > const g = new Foo();
> > console.log(new Foo() === g);
> > ```
>
> Which demonstrates that `Foo` by such a criterion is not a reasonable
> constructor function.  I think that makes sense.  It clearly isn't
> one.
>
> > If the question is "How can an external observer know if a constructor
> > returns `this` vs some other object?" the answer is, you cannot.
>
> No, I don't think that's the central point.  But perhaps the examples
> above do capture something fundamental.  Even if we cannot prove it
> about an arbitrary function, that seems like a minimal criteria for a
> function to be a reasonable constructor:
>
> > A function, `Foo` can be used as a constructor function if
> >   1. every call to `new Foo(...args)` creates a distinct object not
> reference-equal to any other such call.
> >   2. ??
>
> Does that make sense?  Are there other important criteria?
>
> Of course this doesn't help us answer whether a particular function
> actually meets these criteria, but it's a start.
>
>   -- Scott
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170120/4ef2b24b/attachment.html>


More information about the es-discuss mailing list