Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7089271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:54:05+00:00 2026-05-28T07:54:05+00:00

I know $() is the jQuery function… but what does $({}) reference/do? I’m looking

  • 0

I know $() is the jQuery function… but what does $({}) reference/do? I’m looking at Ben Alman’s code in https://gist.github.com/705311

(function(jQuery){
  var o = jQuery({});
  jQuery.each({ 
    "subscribe" : "bind", 
    "unsubscribe" : "unbind", 
    "publish" : "trigger" 
  }, function ( fn, api ) {
    jQuery[ fn ] = function() {
      o[ api ].apply( o, arguments );
    };
  });
})(jQuery);

I’m trying to figure out how this code does what it does. Can someone break it down? Also, what is the jQuery.apply() method? I don’t see it in the jQuery docs – I can only find a 4 year old jQuery apply() plugin, which I doubt is in core now.

There’s a newer, less complicated version at https://gist.github.com/661855, but I’m more curious what/how the 0.X version of this code even worked.

..edit..
I realize that $({}) is an empty object wrapped in jQuery. The question is, why do it, and how does he then end up having created $.subscribe(), $.unsubscribe(), and $.publish() from this tiny snippet of code, especially the o[ api ].apply( o, arguments ); bit?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:54:06+00:00Added an answer on May 28, 2026 at 7:54 am

    This code creates an object to use for the observer pattern. Here’s an explanation line by line:

    // opening a self-executing anonymous function closure
    // assigning global variable jQuery (see last line)
    // to local argument jQuery (usually this would be $)
    (function(jQuery){
    
      // creating a jQuery selector object to access the available methods
      // typeof $.unbind == 'undefined'
      // typeof $({}).unbind == 'function'
      // And more importantly this new object will be the container
      // for our observer pattern (see below)
      var o = jQuery({});
    
      // iterating over an object with new method keys to old method values
      // creating syntactic sugar for an observer pattern (footnote 1)
      jQuery.each({ 
        "subscribe" : "bind", 
        "unsubscribe" : "unbind", 
        "publish" : "trigger" 
    
      // here fn will be the the new method name and api will be the old one
      }, function ( fn, api ) {
    
        // a property of the jQuery namespace is created with the new method name
        jQuery[ fn ] = function() {
    
          // which calls the old method applying given arguments (footnote 2)
          // to our new observer container object
          o[ api ].apply( o, arguments );
    
        };
      });
    })(jQuery);
    

    footnote 1: see http://msdn.microsoft.com/en-us/scriptjunkie/hh201955.aspx

    footnote 2: see http://odetocode.com/blogs/scott/archive/2007/07/05/function-apply-and-function-call-in-javascript.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In jQuery ajax function there is xhr option. Does someone know more details, usability
what does this code performs? if(object.jquery){ .... } I don't know what jQuery attribute
I need to call a jQuery function with EVAL (but I don't know how
I would like to know if I can use the following JQuery function on
I know jQuery does most when it comes to cross-browser issues. I just wanted
Does anyone know of a jQuery plugin that has 'helpers' or extensions like those
Here's a little jQuery function that does what I want when a checkbox is
I made a jQuery function that binds some fields, but it doesn't work when
I would like a jQuery function to know which link was clicked to call
Strange question, I know. Allow me to explain. I have a JavaScript/jQuery function that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.