constructor.name is undefined in Internet Explorer

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

    From matt.scharley.me

    /**
     * Hack in support for Function.name for browsers that don't support it.
     * IE, I'm looking at you.
    **/
    if (Function.prototype.name === undefined && Object.defineProperty !== undefined) {
        Object.defineProperty(Function.prototype, 'name', {
            get: function() {
                var funcNameRegex = /function\s([^(]{1,})\(/;
                var results = (funcNameRegex).exec((this).toString());
                return (results && results.length > 1) ? results[1].trim() : "";
            },
            set: function(value) {}
        });
    }
    

提交回复
热议问题