javascript IE 不支持 function.name。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6903762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:17:45  来源:igfitidea点击:

function.name not supported in IE.

javascript

提问by McKayla

Lately I've become a huge fan of the function.nameproperty.

最近,我已经成为该function.name物业的忠实粉丝。

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

例如,我编写了一个用于扩展原型的函数。

It works in the way of..

它的工作方式是..

Array.give(
    function forEach() { ... }
);

..which would then let you do..

..这会让你做..

['a', 'b', 'c'].forEach(function () { ... });

This code works great in Chrome, Safari, Firefox, and Opera, but not in IE.

这段代码在 Chrome、Safari、Firefox 和 Opera 中运行良好,但不适用于 IE。

After just a small bit of digging, I realized that to the give function, function.namewas just returning undefined, where as in everything else it returned "forEach".

经过一点点挖掘,我意识到给函数,function.name只是返回undefined,而在其他一切中它返回"forEach"

Is there an alternative way to get the name in IE, or should I just fall out of love with this wonderful property?

有没有其他方法可以在 IE 中获得这个名字,还是我应该对这个美妙的属性失去兴趣?

回答by Jürg Lehni

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

您可以使用 Object.defineProperty 在 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;
        }
    });
}

回答by Felix Kling

You might be able to parse the function name from calling function.toString[docs]. function.nameis not a standard property.

您也许可以通过调用function.toString[docs]来解析函数名称。function.name不是一个标准特性

var name = func.toString().match(/^function\s*([^\s(]+)/)[1];


As the comments also say, this is not necessarily a reliable way. Imo passing an object would be easier to read and you could pass several methods at once:

正如评论中所说,这不一定是可靠的方式。Imo 传递一个对象会更容易阅读,你可以一次传递几个方法:

Array.give({
    forEach: function() { ... },
    somethingElse: function() {...}
});

回答by James

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

我认为您的.give()解决方案有点……冗长。有什么问题:

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.name带到这里想知道,所以有一种方法可以获取名称(显然,它不适用于匿名函数):

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

回答by bencergazda

For those, who are in the same boat, have a look at JamesMGreene's Function.namepolyfill. This looks to be a good solution.

对于那些在同一条船上的人,请查看 JamesMGreene 的Function.namepolyfill 。这看起来是一个很好的解决方案。

回答by user2782001

Old question, and I don't know if this works on a native IE 7 & 8 browser (I'm using the developer tools emulator), but I've given functions a name property on the constructor for gag inducing IE code before....

老问题,我不知道这是否适用于本机 IE 7 和 8 浏览器(我使用的是开发人员工具模拟器),但我之前在构造函数上为函数提供了一个名称属性,用于诱导 IE 代码。 ...

function sayMyName(func){
    var name=func.name || func.constructor.name
    alert(name);

}

var ieGivesMeNightTerrors=function(){
    //there there, these ugly workarounds aren't your fault
};
ieGivesMeNightTerrors.constructor.name=ieGivesMeNightTerrors;
sayMyName(myFunc);