A polyfill for the basic functionality of Function.name
accessor property in its pre-ES6 form.
function hello() {
/* ... */
}
console.log(hello.name); // "hello"
var fn = function foo() {
/* ... */
};
console.log(fn.name); // "foo"
Most modern browsers have already supported this basic functionality for quite some time but this polyfill will apply to at least the following:
- IE
>=9 <12
- Chrome
<33
For IE <9
, you can still use fn._name()
instead.
- In short, this means that this polyfilled
name
accessor property can provide you with the name of a named function definition (either a named function declaration or a named function expression). - Unlike other browsers with a similar support level for the pre-ES6 form, this poyfilled
name
accessor property is also intentionally marked as configurable.
- When polyfilling for Chrome
<5
, the accessor property will be configurable (expected) AND enumerable (unexpected) due to having to implement it usingObject.prototype.__defineGetter__
instead ofObject.defineProperty
.