Since ES5 doesn't support Function#name. I was looking for a way to emulate that functionality. While a lot of people recommend using Function#toString, others strongly advised against it.
So what are the risks of using the below code to get the name of a function?
if (!Object.hasOwnProperty(Function.prototype, "name")) {
Object.defineProperty(Function.prototype, "name", {
configurable: false,
enumerable: true,
get: function() {
var result = /function\s+([^\s(]+)/.exec(this.toString());
return result ? result[1] : "";
}
});
}
Since ES5 doesn't support arrow functions, I don't really see when the where the risk lies.