admin管理员组文章数量:1397219
The sample code mentioned below is a part of jQuery Countdown plugin by Keith Wood. Can some explain this
_attachCountdown: function(target, options) {
var $target = $(target);
if ($target.hasClass(this.markerClassName)) {
return;
}
$target.addClass(this.markerClassName);
var inst = {options: $.extend({}, options),
_periods: [0, 0, 0, 0, 0, 0, 0]};
$.data(target, PROP_NAME, inst);
this._changeCountdown(target);
}
Is there a reason specifically defining $target or its just same as our simple variables like var target.
Thanks in advance.
The sample code mentioned below is a part of jQuery Countdown plugin by Keith Wood. Can some explain this
_attachCountdown: function(target, options) {
var $target = $(target);
if ($target.hasClass(this.markerClassName)) {
return;
}
$target.addClass(this.markerClassName);
var inst = {options: $.extend({}, options),
_periods: [0, 0, 0, 0, 0, 0, 0]};
$.data(target, PROP_NAME, inst);
this._changeCountdown(target);
}
Is there a reason specifically defining $target or its just same as our simple variables like var target.
Thanks in advance.
Share Improve this question edited Jul 13, 2012 at 10:21 kapa 78.7k21 gold badges165 silver badges178 bronze badges asked May 27, 2011 at 20:19 Amit GuptaAmit Gupta 5774 silver badges14 bronze badges3 Answers
Reset to default 6It is a simple variable, $
is just added to indicate to the code reader that a jQuery collection is stored inside. Javascript is quite "lenient" with variable names, the $
has no special meaning (opposed to PHP where it is needed before every variable name).
This method (var $target=$(target);
) is used to save the result of $(target)
(the jQuery collection itself, storing target
) into a variable, so the jQuery collection does not need to be created everytime it is needed.
The $
in JavaScript is valid for variable names and has no significance on its functionality.
The original author was probably saving two keystrokes (or four if you include shift) and renaming it for convenience, but left the $
prefix to symbolize it is a jQuery-wrapped object. (Think of it like the old Hungarian Notation facsimile.)
By the following code:
var $target = $(target);
the author of the script assigns to the following variable:
$target
the result of the following expression:
$(target)
which is the result of jQuery()
function ($()
is only an alias for it) being passed the target
variable.
So, to sum up, what you have here is:
target
JS variable (probably some string determining the selector),$
JS function (basicallyjQuery
function, but the$
alias is often used for writing shorter code),$target
JS variable that stores the result of the$(target)
(orjQuery(target)
) expression
本文标签: target(target) in javascriptjQuery Need explanation of syntaxStack Overflow
版权声明:本文标题:$target = $(target) in javascriptjQuery. Need explanation of syntax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744106641a2591093.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论