Coder Social home page Coder Social logo

Comments (21)

jots avatar jots commented on May 20, 2024

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

from coffeescript.

noonat avatar noonat commented on May 20, 2024

I would also love having this.

from coffeescript.

 avatar commented on May 20, 2024

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

from coffeescript.

beastaugh avatar beastaugh commented on May 20, 2024

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

from coffeescript.

liamoc avatar liamoc commented on May 20, 2024

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

from coffeescript.

jashkenas avatar jashkenas commented on May 20, 2024

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.

from coffeescript.

weepy avatar weepy commented on May 20, 2024

:-( 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' ?

from coffeescript.

jashkenas avatar jashkenas commented on May 20, 2024

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?

from coffeescript.

jots avatar jots commented on May 20, 2024

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 :: ).

from coffeescript.

mborejdo avatar mborejdo commented on May 20, 2024

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 ~>

from coffeescript.

weepy avatar weepy commented on May 20, 2024

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.

from coffeescript.

noonat avatar noonat commented on May 20, 2024

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.

from coffeescript.

jots avatar jots commented on May 20, 2024

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

from coffeescript.

jashkenas avatar jashkenas commented on May 20, 2024

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.

from coffeescript.

jots avatar jots commented on May 20, 2024

awesome! Thanks Jeremy!

from coffeescript.

weepy avatar weepy commented on May 20, 2024

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 ??

from coffeescript.

noonat avatar noonat commented on May 20, 2024

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)

from coffeescript.

jashkenas avatar jashkenas commented on May 20, 2024

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.

from coffeescript.

weepy avatar weepy commented on May 20, 2024

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 ?

from coffeescript.

 avatar commented on May 20, 2024

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

from coffeescript.

mborejdo avatar mborejdo commented on May 20, 2024

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 :-)

from coffeescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.