'@' 符号在 Javascript、Coffeescript 或 Jquery 中是否有特殊含义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14143123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does the '@' symbol have special meaning in Javascript, Coffeescript or Jquery?
提问by Noah Clark
I have some code that looks like
我有一些看起来像的代码
self = @
and then later on it's using @someMethodName or self.someMethodName
然后稍后它使用@someMethodName 或 self.someMethodName
Does @ have some special meaning?
@有什么特殊含义吗?
回答by jbabey
@
is not a valid character for a javascript identifier. Identifiers may only contain $
, _
, digits and letters.
@
不是 javascript 标识符的有效字符。标识符只能包含$
、_
、 数字和字母。
In coffeescript, @
means this
.
在 coffeescript 中,@
意思是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.
CoffeeScript 有一些与 this 关键字相关的不错的特性。首先,CoffeeScript 使用 @ 符号作为 this. 的简写。例如,@foo 等价于 this.foo。其次,如果您在函数的参数中使用 @ 符号,CoffeeScript 会自动将这些值指定为对象的属性。
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 questionor the docs.
编辑:就 jQuery 而言,与 javascript 相同的标识符规则适用,因为 jQuery 只是 javascript。对于@
jQuery 中的其他用途,请参阅此问题或文档。
回答by Esailija
@
is shortcut for this
in coffeescript
@
是this
在 coffeescript 中的快捷方式
So
所以
self = @
is coffeescript for:
是咖啡脚本:
var self = this;