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

  • SEARCH
  • Home
  • 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 7438043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:29:38+00:00 2026-05-29T10:29:38+00:00

I am creating an AJAX API for a web service and I want to

  • 0

I am creating an AJAX API for a web service and I want to be able to call jQuery-like accessors.
jQuery seems to be able to execute ‘jQuery’ as a function, but also use it to directly access the object that is the result of the function EG:

jQuery();
jQuery.each({});

This is the trick that I can’t seem to pull off:

myAPI('foo'); //output: 'foo'
myAPI('foo').changeBar(); //output: 'foo' 1
myAPI.changeBar(); //Error: not a function

I have seen the answers to similar questions, which are helpful, but don’t really answer my question.

#8734115 – Really interesting, but you can’t access the methods that were set by f.prototype.

#2953314 – Uses Multiple operations to create object instead of a single function.

here is my code:

(function(window) {

        var h = function(foo) {
                // The h object is actually just the init constructor 'enhanced'
                return new h.fn.init(foo);
        };
        /**
         * Methods defined at protoype.
         */
        h.fn = h.prototype = {
            constructor: h,
            init: function(foo) {
                console.log(foo);
                return this;
            },
            splice : function () {},
            length : 0,
            bar : 0,
            changeBar : function() {
                this.bar++;
                return this.bar;
            }
        };
        h.fn.init.prototype = h.fn;

    //Publish 
    window.myAPI =h;

}( window));

I’m sure I’m missing something simple 🙁

  • 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-29T10:29:39+00:00Added an answer on May 29, 2026 at 10:29 am

    What jQuery is doing there is using jQuery as both a function and as a pseudo-namespace. That is, you can call jQuery: var divs = jQuery("div"); and you can use properties on it, e.g.: jQuery.each(...);.

    This is possible because in JavaScript, functions are first-class objects, and so you can add arbitrary properties to them:

    function foo() {
        alert("Foo!");
    }
    foo.bar = function() {
        alert("Bar!");
    };
    
    foo();     // "Foo!"
    foo.bar(); // "Bar!"
    

    That’s literally all there is to it.

    Within the call to bar, this will be the foo function (because this is determined entirely by how a function is called, not where it’s defined). jQuery doesn’t use this to refer to itself (usually it uses this to refer to DOM elements, sometimes to other things like array elements; when referring to itself, since it’s a single thing, it just uses jQuery).

    Now, you might want to ensure that your functions have proper names (whereas the function I assigned to bar above is anonymous — the property has a name, but the function does not). In that case, you might get into the module pattern:

    var foo = (function() {
        function foo() {
            alert("Foo!");
        }
    
        function foo_bar() {
            alert("Bar!");
        }
    
        foo.bar = foo_bar;
    
        return foo;
    })();
    
    foo();     // "Foo!"
    foo.bar(); // "Bar!"
    

    That pattern also has the advantage that you can have private data and functions held within the scoping function (the big anonymous function that wraps everything else) that only your code can use.

    var foo = (function() {
        function foo() {
            reallyPrivate("Foo!");
        }
    
        function foo_bar() {
            reallyPrivate("Bar!");
        }
    
        function reallyPrivate(msg) {
            alert(msg);
        }
    
        foo.bar = foo_bar;
    
        return foo;
    })();
    
    foo();               // "Foo!"
    foo.bar();           // "Bar!"
    reallyPrivate("Hi"); // Error, `reallyPrivate` is undefined outside of the scoping function
    

    In your code, you’re assigning things to the prototype property of the function. That only comes into play when the function is called as a constructor function (e.g., via new). When you do that, the object created by new receives the function’s prototype property as its underlying prototype. But that’s a completely different thing, unrelated to what jQuery does where it’s both a function and a pseudo-namespace.

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

Sidebar

Related Questions

I am creating a web app which uses jQuery to authenticate: $.ajax({ url: /session/create?format=json,
An ajax call creating problem. It works fine in browser but through ajax it
I am interested in creating an AJAX form submit in a jQuery overlay. I
I'm creating a simple ajax call to retrieve a list of user reviews. I
I'm creating a CMS using jQuery and AJAX. When I click, my Add Campaign
I'm creating a basic form with ajax submit using jquery framework. This is my
I have jQuery.ajax() creating a request to a url (cms2/docman/dir/%id) (%id is a numeric
I am creating a loginform like this: @using (Ajax.BeginForm(Login, new AjaxOptions() { HttpMethod =
I want users to be able to submit nodes and comments via AJAX. I
I am creating an ajax login and it used to work but for some

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.