Coercing 'this'

# David-Sarah Hopwood (17 years ago)

Mark S. Miller wrote:

On Mon, Sep 15, 2008 at 7:28 PM, David-Sarah Hopwood < david.hopwood at industrial-designers.co.uk> wrote:

Agreed that this proposal does not prevent this existing confusion. Neither does it make it worse. If there's something we can plausibly do to fix this, I'd love to! Any suggestions?

The problem here is the lack of any run-time type distinction between constructors and other functions. I don't see how to fix the existing confusion in a compatible way. However, when we add the class feature in ES-Harmony, we should avoid making it worse by using a desugaring that allows the class name to be used with 'new', but prevents any use as a function from breaking object encapsulation.

The desugaring I presented in "Look ma, no 'this'"

esdiscuss/2008-August/006941

does so, precisely because it avoids 'this'.

Yes, so it does.

If we choose any other desugaring (perhaps using 'this' for performance reasons, say), we should make sure that it retains that property.

When you ask for suggestions to prevent the existing confusion, do you mean:

  • the fact that some built-ins have different specified behaviour when used as functions vs as constructors, and this can't be expressed by self-hosting code in all cases?
  • the fact that something intended as a constructor can be called as a function and vice-versa?
  • both?
# Mark S. Miller (17 years ago)

On Mon, Sep 15, 2008 at 8:12 PM, David-Sarah Hopwood < david.hopwood at industrial-designers.co.uk> wrote:

When you ask for suggestions to prevent the existing confusion, do you mean:

  • the fact that some built-ins have different specified behaviour when used as functions vs as constructors, and this can't be expressed by self-hosting code in all cases?
  • the fact that something intended as a constructor can be called as a function and vice-versa?
  • both?

Both.