1

why returning "this" in this function here:

(function($) {

  $.fn.menumaker = function(options) {

      var cssmenu = $(this), settings = $.extend({
        title: "Menu",
        format: "dropdown",
        sticky: false
      }, options);

      return this.each(function() {

this is the "DOM element" ? If we want to use the content of the function, why not referencing $(this) to use the element we are targeting, instead of this?

Thanks

Paul
  • 6,108
  • 14
  • 72
  • 128

1 Answers1

6

jQuery methods (properties of $.fn) aren't like jQuery event handler callbacks. The value of this is the jQuery object itself, not a DOM element.

The $.fn object is the protoype object for jQuery instances, so when you make a jQuery object

var jq = $(something);

then call a jQuery method:

jq.whatever();

the natural this rules of JavaScript determine that whatever() will be invoked with the jQuery object as the value of this.

Pointy
  • 405,095
  • 59
  • 585
  • 614