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.
It 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, storingtarget) into a variable, so the jQuery collection does not need to be created everytime it is needed.