di.js icon indicating copy to clipboard operation
di.js copied to clipboard

bug(providers): methodless class instantiation broken in IE

Open EisenbergEffect opened this issue 9 years ago • 0 comments

This is an IE-only bug.

If you have a class, which has a constructor, but nothing on the prototype, then the FactoryProvider will be used instead of the ClassProvider. This causes this to be undefined in the constructor of the class during instantiation, which usually results in exceptions.

The name property of function is non-standard and not implemented in any version of IE up to now (11). However, you can polyfill name. See this stack overflow discussion for a simple implementation which appears to fix these issues, at least in my tests: http://stackoverflow.com/questions/6903762/function-name-not-supported-in-ie

Providing the polyfill inline here for convenience:

// Fix Function#name on browsers that do not support it (IE):
if (!(function f() {}).name) {
    Object.defineProperty(Function.prototype, 'name', {
        get: function() {
            var name = this.toString().match(/^\s*function\s*(\S*)\s*\(/)[1];
            // For better performance only parse once, and then cache the
            // result through a new accessor for repeated access.
            Object.defineProperty(this, 'name', { value: name });
            return name;
        }
    });
}

EisenbergEffect avatar Sep 11 '14 14:09 EisenbergEffect