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>" + '<>'.de());
To understand what is happening one has to have a very close look at what is actually done.
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!
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 theString.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 puttingentityin the local scope.Now to your question regarding how
aandbcome into play!This is related on how
String.replacebehaves when second parameter passed is a function. The link DCoder provided in the comments explains this pretty good! The parameterais the entire matched substring,bis the first matched “parenthesized submatch”.