Syntax shortcut for accessing properties on the current context ("this")

# Nathaniel Higgins (9 years ago)

In CoffeeScript (and a few other languages including Ruby I believe), there is a syntax shortcut for simplifying accessing properties on the current context (this variable), which is something you have to do often.

This looks something like @prop, which is synonymous with this.prop. I’m not sure if the “@“ character is used for anything else yet in JavaScript, but I’m sure we could find another character to do the same if it is.

--  Nathaniel Higgins Sent with Airmail

# Andrea Giammarchi (9 years ago)

apparently the @ has been booked already for decorators medium.com/google-developers/exploring-es7-decorators-76ecb65fb841

I also personally think we don't really need yet another magic shortcut that but that's just my opinion.

Best

# Nathaniel Higgins (9 years ago)

Ah yes, silly me.

What about %prop or $prop? When it’s something you have to do a lot, repeatedly typing this. can be a pain. There’s definitely a case for it as many other languages use it.

--  Nathaniel Higgins Sent with Airmail

On 27 July 2015 at 14:46:33, Andrea Giammarchi (andrea.giammarchi at gmail.com) wrote:

apparently the @ has been booked already for decorators medium.com/google-developers/exploring-es7-decorators-76ecb65fb841

I also personally think we don't really need yet another magic shortcut that but that's just my opinion.

Best

On Mon, Jul 27, 2015 at 2:23 PM, Nathaniel Higgins <nat at nath.is> wrote:

Hi.

In CoffeeScript (and a few other languages including Ruby I believe), there is a syntax shortcut for simplifying accessing properties on the current context (this variable), which is something you have to do often.

This looks something like @prop, which is synonymous with this.prop. I’m not sure if the “@“ character is used for anything else yet in JavaScript, but I’m sure we could find another character to do the same if it is.

--  Nathaniel Higgins Sent with Airmail

# Andrea Giammarchi (9 years ago)

the real bummer to me is that with statement has been deprecated and killed without a replacement but AFAIK the "dot-stache" operator should come to save us from typing:


this.{
  doStuff();
  doMore();
  prop = Math.random();
  console.log(prop);
}

although I cannot even find the strawman ... and this seems outdated proposal: harmony:object_literals#object_extension_literal

it's to define properties only ... uhm ... if I'll find the proposal, I'll link to it.

Best

# Michael McGlothlin (9 years ago)

I'd rather that prop just match the object the method is actually attached to, regardless of the context of 'this', before continuing searching the tree. Familiar as its done that way in many languages, short, and independent of 'this' context.

A context operator might be a good idea though. Maybe by itself it'd be equiv to this but could also be used to reference the function itself, the attached object, etc. #.prop, #function.prop, #object.prop, etc?

Thanks, Michael McGlothlin Sent from my iPhone

# Brendan Eich (9 years ago)

Michael McGlothlin wrote:

I'd rather that prop just match the object the method is actually attached to, regardless of the context of 'this', before continuing searching the tree. Familiar as its done that way in many languages,

You mean static (applied to types, including fields in objects) languages? Those are not like JS in the critical sense that a free variable reference in a JS method, e.g.

class C { m() { return x; } ... }

should resolve per lexical scope, not per an implicit this. that may or may not apply, depending on whether 'x' in this => true.

If you say it always means this.x then there's no way to refer to an outer lexical variable, x.

So there has to be some prefix, if not this., to distinguish.

# Matthew Robb (9 years ago)

does it seem reasonable though that if properties get initializers in the class body that they could be considered "in scope" to everything else defined in the class body?

# Brendan Eich (9 years ago)

Matthew Robb wrote:

does it seem reasonable though that if properties get initializers in the class body that they could be considered "in scope" to everything else defined in the class body?

That's a big "if", but with the right declarative, binding-like syntax, that's plausibly valid inference. Soundness (the "if" condition's truth) remains to be seen.