Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/14143123/does-the-symbol-have-special-meaning-in-javascript-coffeescript-or-jquery

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

@ 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...