Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the '@' symbol have special meaning in Javascript, Coffeescript or Jquery?

I have some code that looks like

self = @ 

and then later on it's using @someMethodName or self.someMethodName

Does @ have some special meaning?

like image 782
Noah Clark Avatar asked Jan 03 '13 16:01

Noah Clark


People also ask

What does-> mean in CoffeeScript?

In CoffeeScript there are two different types of arrows for defining functions: thin arrows -> and fat arrows =>. The JavaScript function keyword was replaced with the thin arrow. The fat arrow serves as the function keyword and also binds the function to the current context.

Does CoffeeScript compile to JavaScript?

CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.


2 Answers

@ is not a valid character for a javascript identifier. Identifiers may only contain $, _, digits and letters.

In coffeescript, @ means this.

CoffeeScript has a few nice features related to the this keyword. First, CoffeeScript uses the @ symbol as shorthand for this.. For example, @foo is equivalent to this.foo. Second, if you use the @ symbol in the parameters of a function, CoffeeScript will automatically assign those values as properties of the object.

Edit: As far as jQuery is concerned, the same identifier rules as javascript apply since jQuery is just javascript. For other uses of @ in jQuery, see this question or the docs.

like image 146
jbabey Avatar answered Oct 09 '22 02:10

jbabey


@ is shortcut for this in coffeescript

So

self = @ 

is coffeescript for:

var self = this; 
like image 36
Esailija Avatar answered Oct 09 '22 02:10

Esailija