bhb
bhb

Reputation: 2561

Events on empty jquery objects

What is the use of empty jquery object here? And what does it mean when we have an event like on/off on an empty object.

(function($) {

  var o = $({}); //<--

  $.subscribe = function() {
    o.on.apply(o, arguments);
  };

  $.unsubscribe = function() {
    o.off.apply(o, arguments);
  };

  $.publish = function() {
    o.trigger.apply(o, arguments);
  };

}(jQuery));

Code from here

Upvotes: 0

Views: 271

Answers (1)

benhowdle89
benhowdle89

Reputation: 37494

var o = $({});

jQuery allows you to select an empty object which will return it wrapped with all the extra jQuery sugar, ie. triggering and event listening. So you can then trigger and listen for events on this empty (jQuery) object.

Upvotes: 2

Related Questions