function.name not supported in IE.

前端 未结 5 1027
面向向阳花
面向向阳花 2020-11-29 10:01

Lately I\'ve become a huge fan of the function.name property.

For example, I\'ve written a function for extending prototypes.

It works in the w

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 10:39

    You can use Object.defineProperty to add support for it on IE9+

    // Fix Function#name on browsers that do not support it (IE):
    if (!(function f() {}).name) {
        Object.defineProperty(Function.prototype, 'name', {
            get: function() {
                var name = (this.toString().match(/^function\s*([^\s(]+)/) || [])[1];
                // For better performance only parse once, and then cache the
                // result through a new accessor for repeated access.
                Object.defineProperty(this, 'name', { value: name });
                return name;
            }
        });
    }
    

提交回复
热议问题