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 7416855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:34:51+00:00 2026-05-29T07:34:51+00:00

This is related to a question I asked previously here: How to implement chained

  • 0

This is related to a question I asked previously here: How to implement chained method calls like jQuery?

I have been using the method from the check answer from some time there, and it works well. But I would like to alter the syntax for my toolkit even further.

  1. foo(firstarg).bar(secondarg); // should function as the question above.

  2. foo(onlyarg).bar // a function with one argument

  3. foo.bar(onlyarg); // should also work, when a first argument is not appropriate.

  4. foo.bar; // a function without an argument, or returns a static value.

I would like all 4 syntaxs to work off the same foo object, but lack the OOP understanding to do so. I have tried a few things, and so far I can get 1 & 2 to work, and 3 & 4 to work, but not to all work together. It also would be nice if chaining remained an option by having each function return the root object.

Edit: I clearly need to be more specific, here is what I have now:

 var main = function(obj){ this.obj = obj; };
 var tool = function(obj){ return new main(obj); };
 main.prototype = {
      alertThisPlus : function(plus){
           alert(this.obj + ' ' + plus);    
      },
      alertJustThis : function(){
           return alert(this.obj);
      }
 };

usage

 tool('hello').alertThisPlus('world'); // returns alert('hello world')
 tool().alertJustThis('hello world');  // returns alert('hello world');

what I would like is to do this:

 tool('hello').alertThisPlus('world'); // returns alert('hello world') no change
 tool.alertJustThis('hello world');  // returns alert('hello world') does not work
  • 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-29T07:34:52+00:00Added an answer on May 29, 2026 at 7:34 am

    Functions are just objects so you can add functions to ‘tool’. You can do this manually:

    tool.foobar = function() {}; 
    

    Or if your classes are structured suitably you could use a mix-in approach. Something like this:

    function Tool(prefix) {
      this.prefix = prefix;
    }
    
    Tool.prototype.alertThisPlus = function(suffix) {
      alert((typeof this.prefix != 'undefined' ? this.prefix + ' ' : '') + suffix);
    };
    
    Tool.prototype.alertJustThis = function(msg) {
      alert(msg);
    };
    
    function tool(prefix) {
      return new Tool(prefix);
    }
    
    // Mix-in the methods from a static instance of Tool onto the 'tool' function.
    // This makes both tool.alertThisPlus() and tool.alertJustThis() available,
    // both will be called in the context of 'staticTool'.
    (function() {
      var staticTool = new Tool();
      for (var o in staticTool) {
        if (typeof staticTool[o] == 'function') {
          tool[o] = staticTool[o].bind(staticTool);
        }
      }
    })();
    
    tool('hello').alertThisPlus('world'); // returns alert('hello world')
    tool().alertJustThis('hello world');  // returns alert('hello world')
    tool.alertJustThis('hello world');    // returns alert('hello world')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a related to a previous question I have asked here, see the
This question is related to one I asked previously, see here . As a
This is related to a previous question I asked with Jquery. If I have
This question is related to two different questions I have asked previously: 1) Reproduce
This is related to a question I asked here: Thread Locking in Ruby (use
This question is related to another question I asked Basically, I have 2 horizontally
This is related to my question asked here today on SO. Is there a
I asked a question related to this one previously but I need to know
This is closely related to a previous question i asked. I have a many-to-many
This question is related to my earlier question, asked here: How do I get

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.