Comments (21)
I like it. "@" is my fave between the 3.
from coffeescript.
I would also love having this.
from coffeescript.
I hate "prototype" keyword. "@" is a good idea.
from coffeescript.
@
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.
I'm an advocate for :: for prototype and @ for this
from coffeescript.
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.
:-( 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 forthis.
, so writing@legs
is the same as writingthis.legs
Tortoise: Ah! That's handy.
As it's entirely optional, perhaps it could be an 'advanced feature' ?
from coffeescript.
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.
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.
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.
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.
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.
I agree, having to think about what kind of ":" is a major negative.
from coffeescript.
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.
awesome! Thanks Jeremy!
from coffeescript.
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.
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.
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.
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.
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.
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)
- Bug?: Cannot Base64 encode value: 0 (Legacy browser/WSH) HOT 2
- Unnecessary `splice` ref added for Array destructuring with rest element not in last position HOT 4
- Bug: Re-ordered nested non-end BindingRestElement doesn't get transpiled HOT 1
- CoffeeScript is fantastic, please donβt give it up HOT 1
- How to imitate `let` behavior in loops? HOT 5
- Proposal: Alternative file extension HOT 1
- Bug: Excessive variable and shallow copy for leading or middle rest parameter
- Proposal: Introduce `let` statement. HOT 6
- Proposal: Document Existential Operator Assignment
- Site issue: code blocks twitch on hover HOT 2
- Proposal: cake command should support ES6 modules HOT 2
- Need help understanding class member meanings HOT 1
- CLI `npm` `scripts` and input `.coffee` file/s as last argument conflicting with `--watch` HOT 2
- Bug: wrong code is transpiled for function call without parentheses HOT 2
- Proposal: Add end word to close method or class HOT 2
- Bug: Invalid indentation allowed after `do`
- Bug: Remove checkShebangLine multi arguments check HOT 1
- feature_request(html): backend CoffeeScript compilation inside HTML files HOT 3
- Bug: yield cannot be used in do -> expressions reliably? HOT 5
- [not an issue] An embeddable playground for CoffeeScript HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coffeescript.