jquery variable syntax
This question already has an answer here:
$self has 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.
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.
This is pure 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 .
It's ugly, dirty, confusing, but $ , self and $self are all different variables that happen to have similar names.
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.
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:
var $this = $(this);
链接地址: http://www.djcxy.com/p/94948.html
上一篇: 何时/为什么在使用jQuery时为变量添加前缀“$”?
下一篇: jQuery的可变语法
