'@' 符号在 Javascript、Coffeescript 或 Jquery 中是否有特殊含义?
我有一些看起来像的代码
I have some code that looks like
self = @
然后它使用 @someMethodName
或 self.someMethodName
@
有什么特殊含义吗?
推荐答案
@
不是 javascript 标识符的有效字符.标识符只能包含 $
、_
、数字和字母.
@
is not a valid character for a javascript identifier. Identifiers may only contain $
, _
, digits and letters.
在咖啡脚本中,@
表示 这个
.
In coffeescript, @
means this
.
CoffeeScript 有一些与 this 关键字相关的不错的功能.首先,CoffeeScript 使用 @ 符号作为这个的简写.例如,@foo 等价于 this.foo.其次,如果你使用@函数参数中的符号,CoffeeScript 将自动将这些值分配为对象的属性.
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.
就 jQuery 而言,与 javascript 相同的标识符规则适用,因为 jQuery 只是 javascript.对于 jQuery 中 @
的其他用途,请参阅 this question 或 文档.
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.
相关文章