function.name not supported in IE.

前端 未结 5 1004
面向向阳花
面向向阳花 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条回答
  •  借酒劲吻你
    2020-11-29 10:57

    I think your .give() solution is a little..verbose. What's wrong with:

    Array.prototype.forEach = function () { ... };
    

    ?

    Really, though, you should check for such a method's existence before supplying your own:

    Array.prototype.forEach = Array.prototype.forEach || function () { ... };
    

    Since others will be led here wondering about function.name, there is a way to grab the name (obviously, it doesn't work on anonymous functions):

    function getFnName(fn) {
        return (fn.toString().match(/function (.+?)\(/)||[,''])[1];
    }
    

提交回复
热议问题