The problem I encountered in the butterfly book, about the immediate execution of the function

Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};
String.method("deentityify", function() {  //
    var entity = {
        quot: """,
        lt: "<",
        gt: ">"
    };
    return function() {
        return this.replace(/&([^&;]+);/g,
                            function(a, b) { //replace 
            var r = entity[b];
            return typeof r === "string" ? r : a;
        });
    };
}());  //

see the problem encountered in the fourth chapter module section in the butterfly book. Isn"t this function an anonymous function? as far as I understand it, you should add parentheses outside the function function to this

.
String.method("deentityify", (function() {  //
    var entity = {
        quot: """,
        lt: "<",
        gt: ">"
    };
    return function() {
        return this.replace(/&([^&;]+);/g,
                            function(a, b) {
            var r = entity[b];
            return typeof r === "string" ? r : a;
        });
    };
})());  //

this can also be performed, and the result is the same, but what"s the difference?
whether this is the case for a function without parentheses, it executes when executing the following sentence, so that it is a function expression and does not need parentheses.

this.prototype[name] = func;

Please give me some advice, thank you!

Apr.02,2021

the one without parentheses is a function expression, but it has been executed before assignment

this.prototype[name] = func;
// func  
function() {
    return this.replace(/&([^&;]+);/g,
                        function(a, b) {
        var r = entity[b];
        return typeof r === 'string' ? r : a;
    });
};

clipboard.png
if you take a look at the above picture, it is an effect. Usually write (function () {.}) () form is to prevent browser parsing errors, directly function () {.} () to run this must report an error, the browser thinks you are defining the method. But put it in the parameter or add another symbol in front of it to make the browser think that the expression will execute

directly.

function expressions that are self-executed or called immediately
are simply understood. First, the function is parenthesized () to represent an expression, and there are two ways to write it (there is no difference in the effect achieved):

// 
(function () { /* code */ } ());  
// 
(function () { /* code */ })();

in fact, there are some in many books, as long as you make the parser think that this is an expression, it will be executed, such as the unary operator:

+function () { /* code */ } ();
-function () { /* code */ } ();
!function () { /* code */ } ();
~function () { /* code */ } ();

the second argument to replace is the function
it is recommended to read the official documentation.
first of all, there is no fixed number of parameters for this method, but there are certain rules

    The first parameter of
  • is the string that is regularly matched each time, and this is fixed. The an above is the
  • of each regular match. The
  • is followed by the content matched by the capture group, the subexpression. The b above is the content in the previous regular brackets
  • then comes the subscript index index
  • that matches the beginning of the string.
  • followed by the source string
Menu