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

匿名 (未验证) 提交于 2019-12-03 01:49:02

问题:

I have some code that looks like

self = @ 

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

Does @ have some special meaning?

回答1:

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



回答2:

@ is shortcut for this in coffeescript

So

self = @ 

is coffeescript for:

var self = this; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!