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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:53:00+00:00 2026-05-25T00:53:00+00:00

I just experienced a behavior in JS which I couldn’t understand: I wanted to

  • 0

I just experienced a behavior in JS which I couldn’t understand: I wanted to create a method which calls String.prototype.replace with some args given, therefore I came up with that:

String.prototype.replace.bind("foo", /bar/g, function(){}).call

I guessed that I would get a function where I would just have to throw a string into to get my replace. Instead, I always get the initial this-value (in this case foo) returned.

Now my questions are:

  • Why is JS behaving like that? What does bind really return and how can I get the this-parameter of .call()?
  • Is there another way of doing what I want to do without creating a wrapper function?
  • 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-25T00:53:01+00:00Added an answer on May 25, 2026 at 12:53 am

    Why it’s happening

    You pass some this value from call to the function returned by bind. bind, however, ignores that value and calls the original function with the bound this (e.g. foo). In fact, bind is meant to bind the this value. The arguments you can bind is rather something additional.

    Solving it

    Without a wrapper function I don’t think you can do what you want. However, with a wrapper function you could do what you’re after:

    Function.prototype.bindArgs = function() {
        var args = arguments,
            func = this;
    
        return function(context) {
            return func.apply(context, args);
        }
    };
    

    E.g.

    var func = function(a, b, c) {
        console.log(this, a, b, c);
    };
    
    var bound = func.bindArgs(1, 2, 3);
    bound([1]); // [1] 1 2 3
    bound({a: 1}); // {a: 1} 1 2 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've experienced rather strange behavior of JSTL forEach tag. I have some bean called
I experienced some odd behavior while using C++ type traits and have narrowed my
I was just wondering how many experienced programers out there actually map out their
I just wanted to know your experience with using resharper. We have a very
My PHP experience is rather limited. I've just inherited some stuff that looks odd
I am experiencing some very strange behavior with Blend: Since weeks i am working
Just today I noticed a strange behavior in an object model that was previously
I have a simple WCF service which accept some data (wsHttpBiding) and returns a
I've just come across a table in production which has 4 foreign key constraints.
The following is some background info on this post. You can just skip to

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.