constructor.name is undefined in Internet Explorer

前端 未结 6 1299
孤独总比滥情好
孤独总比滥情好 2021-02-13 05:11

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

I created the following simple code that reproduces the issu

6条回答
  •  我寻月下人不归
    2021-02-13 05:20

    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

    Update

    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]"
    

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

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

提交回复
热议问题