Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shortcut syntax for this. and prototype. #37

Closed
weepy opened this issue Jan 5, 2010 · 24 comments
Closed

shortcut syntax for this. and prototype. #37

weepy opened this issue Jan 5, 2010 · 24 comments

Comments

@weepy
Copy link

weepy commented Jan 5, 2010

Suggestion, as this. and prototype. are typed alot in JS, it would be useful to have a shortcut.
E.g.

@var  compiles to this.var
Animal@move compiles to Animal.prototype.move

Other suggestions were to use .. or ::. @ has some parity with Ruby.

@jots
Copy link

jots commented Jan 6, 2010

I like it. "@" is my fave between the 3.

@noonat
Copy link
Contributor

noonat commented Jan 7, 2010

I would also love having this.

@ghost
Copy link

ghost commented Jan 8, 2010

I hate "prototype" keyword. "@" is a good idea.

@beastaugh
Copy link

@ makes sense for this, but I would prefer a different symbol for the two cases. Perhaps @varthis.var and Animal..moveAnimal.prototype.move.

@liamoc
Copy link

liamoc commented Jan 10, 2010

I'm an advocate for :: for prototype and @ for this

@jashkenas
Copy link
Owner

I've added :: as shorthand for .prototype. on the current master. It reads pretty well:

Horse::move: =>
    alert("Galloping...")

I'm leaving @ for this out, because referencing properties of this is a fundamental JS idea that I'd rather not obscure, and because it's not too verbose (as opposed to .prototype., which gets rather unwieldy).

Closing the ticket.

@weepy
Copy link
Author

weepy commented Jan 11, 2010

:-( aah that's a shame about @.

I think that the main usefulness is not so much todo with the typing, but with it's ease of reading, e.g.:

x: if this.sq then this.sq[0]*this.sq[1] else null
vs
y: if @sq then @sq[0]*@sq[1] else null

It seems that quite a few of us 'early-adopters' liked it a lot and while I understand that you don't want it to obscure some fundamentals, particularly for newcomers (which I think is a good goal to aim for), it's easy to understand and explain:

Tortoise: what does this @ symbol mean?
Archilles: It's a shortcut for this., so writing @legs is the same as writing this.legs 
Tortoise: Ah! That's handy.

As it's entirely optional, perhaps it could be an 'advanced feature' ?

@jashkenas
Copy link
Owner

Reopening this ticket. My apologies for closing it in the first place. Before we implement @, let's consider the options:

this.property

@property

/property

.property

~property

:property

Just .property would be nice, which would simply make the this implicit. The downside is that you'd have to re-arrange your jQuery-style chains to look this way, instead of dot-first:

$('.row').
  show().
  hide().
  highlight()

Are there other precedents from other languages, apart from @? Which is your favorite?

@jots
Copy link

jots commented Feb 2, 2010

I still like "@". "/" could be confused with divide "." is too hard to see (yeah i probably need glasses :-) ) ~ is a pain to type and reminds me of the bitwise operator. would :property work? if so, that would be my second choice only because it's a bit hard to see (i like it because it seems related to :: ).

@mborejdo
Copy link

mborejdo commented Feb 2, 2010

if you have
:property
on the list, i'd add
=property
too. could also be mistaken with assignment, but would be kindof inline with

=> 

(this binding)

or we go the ~ way and also change => to ~>

@weepy
Copy link
Author

weepy commented Feb 2, 2010

The advantage @ has over the others is that it's relatively heavyweight in terms of size and pixel density, so easy to spot. Secondly, it doesn't have any other use in the language, so wouldn't be confused with other uses.

Regarding standalone @, I suppose it should be equivalent to this.

@noonat
Copy link
Contributor

noonat commented Feb 3, 2010

I like : a lot because it already has the common JSON meaning of a property key.

I also like @ because it's highly readable and has some common meaning, albeit with a different language.

~ also seems like it could work... highly readable and not really overloaded. It also has the meaning of home in nix systems, which I guess is somewhat analogous to the this keyword... maybe. It's a bit more of a reach for my fingers, though, if I'm going to be typing something a lot.

I think . breaking jQuery syntax is probably a pretty big reason to say no to that. You could use '..' which would be easier to read and imply something before it, but that seems to clash with ranges.

As for /... well, I don't have a good reason really, but it just feels weird to me. It's already used for comments, division, and regular expressions.

@jots
Copy link

jots commented Feb 3, 2010

I agree, having to think about what kind of ":" is a major negative.

@jashkenas
Copy link
Owner

Cool. Thanks for all the votes. @property is now on master (and node). Here's the test case:

bob: {
  name:  'Bob'
  greet: (salutation) ->
    salutation + " " + @name
  hello: ->
    @greet "Hello" 
}

puts bob.hello() is "Hello Bob"

It can be used for method calls and property accesses, as well as the start of a longer chain: @property.value compiles to this.property.value.

I left in the naked @ as a reference to this, but I think it may up being poor style to use it -- we tread towards Perl that way.

Closing the ticket.

@jots
Copy link

jots commented Feb 3, 2010

awesome! Thanks Jeremy!

@weepy
Copy link
Author

weepy commented Feb 3, 2010

Lovely :) Off Topic javascript question => Can someone explain how the this is bound to the bob object? The code does indeed, work, but my gut understanding was that this would be bound to window ??

@noonat
Copy link
Contributor

noonat commented Feb 3, 2010

It's bound by calling context. The function is invoked via bob's dot operator, so it's bound to him.

In other words, bob.hello() is equivalent to bob.hello.call(bob)

@jashkenas
Copy link
Owner

Weepy: What you mentioned is the absolute worst part of JavaScript -- the dynamic scope that this has, unlike all other variables, which retain their lexical scope. Harmony is considering fixing it.

Background reading:
http://en.wikipedia.org/wiki/Possession_(linguistics)

One last alternative that's worth thinking about is using my, instead of @, for readability.

Stan's example from the commit becomes:

Animal: (name) ->
  my name: name

horse: new Animal('Bob')

My test from above becomes:

bob: {
  name:  'Bob'
  greet: (salutation) ->
    salutation + " " + my name
  hello: ->
    my greet "Hello" 
}

puts bob.hello() is "Hello Bob"

Clearly would work better for properties than for methods. my respond(), my upload(file) all look quite strange. So, not going to do it, but wanted to get it down in ink.

@weepy
Copy link
Author

weepy commented Feb 4, 2010

From what I've read in other explorations in the issues list, I'm guessing that it would be hard to 'fix' the lousy dynamic scope of this in a sensible way, i.e with another keyword ?

@documentcloud
Copy link

That's actually what the fat arrow => is for. It's just a function where this is bound to lexical instead of dynamic scope.

@mborejdo
Copy link

mborejdo commented Feb 4, 2010

having @ now, makes me feel the fat-arrow could be confusing.

what about something like

-@> 

for binding this

and adding the option to bind to any given scope

-somescope>

or with parens for readability?

-(@)>
-(scope)>

just some thaughts and maybe pointless :-)

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants