constructor.name is undefined in Internet Explorer

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

    This is an improvisation from Oliver's answer.

    Instead of using regex, this method will parse the string once and save it to the scope to avoid performance problem if it's called more than once.

    if(Function.prototype.name === undefined){
        Object.defineProperty(Function.prototype, 'name', {
            get:function(){
                if(!this.__name)
                    this.__name = this.toString().split('(', 1)[0].split(' ')[1];
                return this.__name;
            }
        });
    }
    

提交回复
热议问题