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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:28:48+00:00 2026-06-13T11:28:48+00:00

I am testing module example from JavaScrit: The Good Parts. I don’t know who

  • 0

I am testing module example from JavaScrit: The Good Parts. I don’t know who passes in a and b in function(a, b). Somehow it works.

Function.prototype.method = function(name, f) {
    this.prototype[name] = f;
    return this;
}
String.method('de', function() {
    var entity = {
        lt : '<',
        gt : '>'
    };
    return function() {
        return this.replace(/&([^&;]+);/g, function(a, b) {
            document.write("<br> a=" + a + " b=" + b);
            var r = entity[b];
            return typeof r === 'string' ? r : a;
        });
    };
}());
document.write("<br>" + '&lt;&gt'.de());
  • 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-13T11:28:49+00:00Added an answer on June 13, 2026 at 11:28 am

    To understand what is happening one has to have a very close look at what is actually done.

    Function.prototype.method = function(name, f) {
        this.prototype[name] = f;
        return this;
    }
    

    This part is pretty clear, it is “shortcut” to extend the Prototype of any object.

    The magic happens in the second part of your code, the previously defined function is supplied with an self-executing function as the second parameter!

    String.method('de', function() {
        var entity = {
            lt : '<',
            gt : '>'
        };
        return function() {
            return this.replace(/&([^&;]+);/g, function(a, b) {
                // "this" will refer to the object the function is bound to.
                document.write("<br> a=" + a + " b=" + b);
                var r = entity[b];
                return typeof r === 'string' ? r : a;
            });
        };
    }()); <-- The magic happens here!
    

    This means, the JavaScript interpreter will use the return value of the “first inner” function (the one directly supplied to String.method) as the actual function, since the “first inner” function is executed before assigning it to the String.prototype.

    The code below does the same but puts one more variable (de) in the local scope. The approach above does not pollute the scope, it is a more elegant way to achieve this. You could even unwrap it further putting entity in the local scope.

    var de = function() {
        var entity = {
            lt : '<',
            gt : '>'
        };
        return function() {
            return this.replace(/&([^&;]+);/g, function(a, b) {
                // "this" will refer to the object the function is bound to.
                document.write("<br> a=" + a + " b=" + b);
                var r = entity[b];
                return typeof r === 'string' ? r : a;
            });
        };
    };
    String.method('de', de());
    

    Now to your question regarding how a and b come into play!
    This is related on how String.replace behaves when second parameter passed is a function. The link DCoder provided in the comments explains this pretty good! The parameter a is the entire matched substring, b is the first matched “parenthesized submatch”.

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

Sidebar

Related Questions

I was considering reassigning some functions from a standard library module in my testing
I have the following function which I do unit testing with doctest. from collections
I'm trying to extract functions and function headers from some source code files. Here's
We are using the wonderful FSUnit for our unit testing. This works fine, except
I need help with re module. I have pattern: pattern = re.compile('''first_condition\((.*)\) extra_condition\((.*)\) testing\((.*)\)
How can a Python program easily import a Python module from a file with
I am working on a module that supplies methods for navigating directories and manipulating
I am using JustMock to mock interfaces for unit testing, but perhaps I'm not
I can't seem to get the nose testing framework to recognize modules beneath my
I'm trying to build a test framework for automated web testing in Selenium and

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.