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
[email protected]
https://mail.mozilla.org/listinfo/es-discuss