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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:41:49+00:00 2026-06-13T22:41:49+00:00

Say we have the following sample Javascript code: var modeller = foo; var provider1

  • 0

Say we have the following sample Javascript code:

var modeller = "foo";
var provider1 = function (modeller, content, optionalArg) {
    alert("this: " + this + ", content: " + content + ", optionalArg: " + optionalArg);
}.bind(null, modeller);

var provider2 = function (content, optionalArg) {
    alert("this: " + this + ", content: " + content + ", optionalArg: " + optionalArg);
};

var createConsumer = function () {
    // some arbitrary private vars
    var content = 1; 
    var option = 2; 

    var doConsume = function(provider) {
        provider.apply(this, [content, option])
    };
    return {consume: doConsume};
};

// Now use them
var consumer = createConsumer();

consumer.consume(provider1);
consumer.consume(provider2);

This is simplified a lot for demo purposes, but the gist is that provider1 is already bound, and provider2 is not – the consumer cannot pass itself as the this argument for provider1.

The questions: Is there a way to detect this kind of case where a function is already bound? Is there a way to get provider1 to use the consumer as this? Assuming the answer is no, what is the best way to work around this sort of situation?

  • 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-06-13T22:41:51+00:00Added an answer on June 13, 2026 at 10:41 pm

    “Is there a way to detect this kind of case where a function is already bound?”

    No.

    “Is there a way to get provider1 to use the consumer as this?”

    No.

    “…what is the best way to work around this sort of situation?”

    Since it seems like you want to bind an argument but not the this value, I would make an argument binder method on Function.prototype that returns a function with only the arguments bound, and not this.

    Function.prototype.argBind = function() {
        var _slicer = [].slice,
            orig_args = _slicer.call(arguments),
            orig_func = this;
        return function() {
            return orig_func.apply(this, orig_args.concat(_slicer.call(arguments)));
        };
    };
    

    Then you’d use it like this:

    var modeller = "foo";
    var provider1 = function (modeller, content, optionalArg) {
        alert("this: " + this + ", content: " + content + ", optionalArg: " + optionalArg);
    }.argBind(modeller);
    

    Now the provider1 function will be invoked with whatever normal this value is given, but the modeller will be bound as the first argument.

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

Sidebar

Related Questions

Let's say I have the following, very simple block of code: <script> function hasVal(field)
Let's say I have the following code: typedef std::function<void ()> func_type; void some_func() {
i have JavaScript components, that has following architecture: var MyComponent = function(params) { setup(params);
Let's say I have a following string from twitter: This is my sample test
Let's say I have the following code: $(function () { $(.buy-it-now.ribbon).click(function () { $(.bid-to-beat.ribbon.active).removeClass(active);
Say you have a webpage with following sample code: <tr class=even> <td>something1</td> <td colspan=1>somthing1.1</td>
Lets say we have following sample code in C#: class BaseClass { public virtual
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is
Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =

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.