在Internet Explorer中未定义builder.name

问题描述:

我在IE中的调试工作今天结束,因为发现 constructor.name undefined

My debugging work in IE ended today by finding that constructor.name is undefined.

我创建了以下简单的代码来重现该问题:

I created the following simple code that reproduces the issue:

({}).constructor.name === undefined // => true

是否有任何变通办法可以使此项工作?

Is there any workaround to make this work?

也许以某种方式覆盖了原型?

Maybe overriding somehow the prototype?

如果可能的话,我不想更改语法,因为更改会很重要。

If possible, I don't want to change the syntax, because the change would be major.

JSFIDDLE

问题仅仅是Internet Explorer不支持函数对象的 name 属性。该属性是非标准属性(至少在ECMAScript 6之前),因此并不令人惊讶。

The problem is simply that the name property of function objects is not supported in Internet Explorer. The property is non-standard (up until ECMAScript 6, at least) so it's not altogether surprising.

没有完全可靠的解决方法,因此我建议尝试如果可能,请不要这样做。但是,您也许可以从函数的字符串表示中提取名称。这里有一些我通过快速搜索得到的与此相关的链接:

There isn't a completely reliable workaround so I would suggest trying to do without it if possible. However, you may be able to extract the name from the string representation of the function. Here a couple of links that deal with this that I got from a quick search:

  • Javascript get Function Name?
  • https://gist.github.com/dfkaye/6384439

更新

从评论中可以看出,问题作者的目标是测试变量是否是对普通文本的引用对象由 Object 构造函数创建。对变量 a 执行此操作的可靠方法是

From the comments, it turns out that the goal of the question author is to test whether a variable is a reference to a plain object create by the Object constructor. A reliable way of doing this for a variable a is

Object.prototype.toString.call(a) == "[object Object]"

有关更多信息,我建议由Angus Croll撰写的以下页面:

For more information I recommend the following page written by Angus Croll:

> http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/