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!