1

I use Aptana to write JS and I recently installed jQuery bundle (mostly for code-completion but also for new cool things that I do not know).

So when I type ready and press Ctrl + <Space> I get two options, document ready and document ready (safe) choosing one of which will automatically put $(document).ready(function(){}) or jQuery(document).ready(function($){}) respectively.

But that is irrelevant. When I use $(someElement).cl the code-completion feature gives me the option click and when I select it it puts down the following code for me:

$(someElement).click(function() {
  ${0:
\}});

(It's relevant. I'll talk about it later)

Similarly, I was trying to write a plugin and I typed plugin and pressed Ctrl + <Space> and selecting the option plugin (method) I got the following code:

;(function($) {
}
$.fn.pluginName = function(options) {
  var opts = $.extend({}, $.fn.pluginName.defaults, options);

  return this.each(function() {
    var $this = $(this);

    ${6:
    var o = $.meta ? $.extend({\}, opts, $this.data()) : opts;
  }

  });

  // private function for debugging
  function debug($obj) {
    if (window.console && window.console.log) {
      window.console.log($obj);
    }
  }
};

// default options
$.fn.pluginName.defaults = {
  defaultOne:true,
  defaultTwo:false,
  defaultThree:'yay!'
};

})(jQuery);

First of all, as far as I know, a common method of writing a plugin goes like this

(function($) {
   $.fn.plugin = function(args) {
      //...
   }
})(jQuery);

And I can define defaults as $.fn.plugin.defaults = {...} or just var defaults = {...} inside my plugin and in my plugin, extend a {} with defaults and args. That's just about all I am familiar with.

So:

  • Why does Aptana jQuery bundle put a ; in the very beginning?
  • Why does it close the function($){} before $.fn.pluginName?
  • And most of all, what is this ${0: in the $.click() example and ${6: in plugin (method) example?
  • Also, what indeed is $.meta and $this.data()?

It's all very confusing (yet, strangely exciting)...

4

1 回答 1

1

然后每个p类都将获得该函数的副本,并$this.data()包含p当前“this”的内容

我不是 jQuery 专家,所以我希望有更多消息灵通的人来帮助解决我无法很好解释的事情。

于 2013-04-01T15:25:49.577 回答