constructor.name is undefined in Internet Explorer

前端 未结 6 1285
孤独总比滥情好
孤独总比滥情好 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:24

    I wrote this for my needs (acknowledging it may not be full-proof):

    if (!Object.constructor.prototype.hasOwnProperty('name')) {
        Object.defineProperty(Object.constructor.prototype, 'name', {
            get: function () {
                return this.toString().trim().replace(/^\S+\s+(\w+)[\S\s]+$/, '$1');
            }
        })
    }
    

    The regex being performed in the method is making an assumption that the 2nd non-whitespace group of characters IS the name of the constructor. This is a viable solution for the code that I'm working with, while this may not be full-coverage for all others' needs.

提交回复
热议问题