jquery 变量语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1916584/
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
jquery variable syntax
提问by dnagirl
I'm learning jQuery by trying to understand other people's code. I ran into this:
我正在通过尝试理解其他人的代码来学习 jQuery。我遇到了这个:
jQuery.fn.myFunc = function(options, callback) {
//stuff
jQuery(this)[settings.event](function(e) {
var self = this,
$self = jQuery( this ),
$body = jQuery( "body" );
//etc.
}
//more stuff
}
My understanding is that $
refers to the jQuery object. So why put $
with $self
and $body
? And is self
the same as $self
?
我的理解是$
指的是 jQuery 对象。那么为什么要$
加上$self
and$body
呢?和self
一样$self
吗?
回答by David Hellsing
$selfhas little to do with $, which is an alias for jQuery in this case. Some people prefer to put a dollar sign together with the variable to make a distinction between regular vars and jQuery objects.
$self与$几乎没有关系,在这种情况下,它是 jQuery 的别名。有些人喜欢将美元符号与变量放在一起,以区分常规变量和 jQuery 对象。
example:
例子:
var self = 'some string';
var $self = 'another string';
These are declared as two different variables. It's like putting underscore before private variables.
它们被声明为两个不同的变量。这就像在私有变量之前放置下划线。
A somewhat popular pattern is:
一个有点流行的模式是:
var foo = 'some string';
var $foo = $('.foo');
That way, you know $foo is a cached jQuery object later on in the code.
这样,您就知道 $foo 是稍后在代码中缓存的 jQuery 对象。
回答by Quentin
This is pure JavaScript.
这是纯 JavaScript。
There is nothing special about $
. It is just a character that may be used in variable names.
没有什么特别之处$
。它只是一个可以在变量名中使用的字符。
var $ = 1;
var $$ = 2;
alert($ + $$);
jQuery just assigns it's core function to a variable called $
. The code you have assigns this
to a local variable called self
and the results of calling jQuery with this
as an argument to a global variable called $self
.
jQuery 只是将它的核心功能分配给一个名为$
. 您分配this
给一个局部变量的代码self
和调用 jQuery 的结果this
作为一个全局变量的参数$self
。
It's ugly, dirty, confusing, but $
, self
and $self
are all different variables that happen to have similar names.
它是丑陋的、肮脏的、令人困惑的,但是$
,self
并且$self
都是碰巧具有相似名称的不同变量。
回答by BalusC
No, it certainly is not. It is just another variable name. The $()
you're talking about is actually the jQuery core function. The $self
is just a variable. You can even rename it to foo
if you want, this doesn't change things. The $
(and _
) are legal characters in a Javascript identifier.
不,当然不是。它只是另一个变量名。在$()
你所谈论的实际上是jQuery的核心功能。该$self
仅仅是一个变量。foo
如果需要,您甚至可以将其重命名为,这不会改变事情。在$
(和_
)是一个JavaScript标识符合法字符。
Why this is done so is often just some code convention or to avoid clashes with reversed keywords. I often use it for $this
as follows:
为什么这样做通常只是一些代码约定或避免与反向关键字发生冲突。我经常使用它$this
如下:
var $this = $(this);
回答by Rich
self and $self aren't the same. The former is the object pointed to by "this" and the latter a jQuery object whose "scope" is the object pointed to by "this". Similarly, $body isn't the body DOM element but the jQuery object whose scope is the body element.
self 和 $self 不一样。前者是“this”指向的对象,后者是一个jQuery对象,其“范围”是“this”指向的对象。类似地,$body 不是 body DOM 元素,而是其作用域是 body 元素的 jQuery 对象。
回答by Jan
The dollarsign as a prefix in the var name is a usage from the concept of the hungarian notation.