IE中不支持function.name。 [英] function.name not supported in IE.
问题描述
最近我成了 function.name
属性的忠实粉丝。
例如,我已经编写了扩展原型的功能。
它的工作原理是..
Array.give (
函数forEach(){...}
);
..这样就可以让你做..
['a','b','c']。forEach(function(){...});
此代码适用于Chrome,Safari,Firefox和Opera,但不适用于IE。
经过一小段挖掘后,我意识到对于给定函数, function.name
刚刚返回 undefined
,返回的其他地方forEach
。
<是否有另一种方法可以在IE中获取名称,或者我是否会对这个美妙的财产失去爱?
您可以通过调用 <$ c $来解析函数名称。 c> function.toString [docs] 。 function.name
是不是标准属性。
var name = func.toString()。match(/ ^ function\s *([^ \s(] +)/)[1];
正如评论所说,这不一定是一种可靠的方式.Imo传递对象会更容易阅读,你可以一次传递几种方法:
Array.give({
forEach:function(){...},
somethingElse:function(){...}
});
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 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.
After just a small bit of digging, I realized that to the give function, function.name
was just returning undefined
, where as in everything else it returned "forEach"
.
Is there an alternative way to get the name in IE, or should I just fall out of love with this wonderful property?
You might be able to parse the function name from calling function.toString
[docs]. function.name
is not a standard property.
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:
Array.give({
forEach: function() { ... },
somethingElse: function() {...}
});
这篇关于IE中不支持function.name。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!