最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

$target = $(target) in javascriptjQuery. Need explanation of syntax - Stack Overflow

programmeradmin5浏览0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 6

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, 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 (basically jQuery function, but the $ alias is often used for writing shorter code),
  • $target JS variable that stores the result of the $(target) (or jQuery(target)) expression
发布评论

评论列表(0)

  1. 暂无评论