Mike Taj
Mike Taj

Reputation: 21

IE11 Javascript behavior with polyfilled Function.name

I have run into a strange behavior in IE11 JavaScript JIT.

Calling Function.name on Classes fails if it is called in the order of base class->child. If called in the child->base class order, it succeeds.

This example:

console.log(Animal.name);
console.log(Snake.name); // returns Animal in IE11
console.log(Horse.name); // returns Animal in IE11

However:

console.log(Snake.name); // returns Snake in IE11
console.log(Horse.name); // returns Horse in IE11
console.log(Animal.name);

Note: changing the order of the children doesn't have any effect in this example.

I have a simple class hierarchy example which I built using Typescript, which is transpired into es6, then babel'd into es5.

The sample page includes the babel polyfill and the script.

I've also tried the JamesMGreene/Function.name polyfill.

Based on walking the code I don't think that is the problem. This appears to be a flaw in the IE11 JIT.

I'm looking for any advice on how to handle this besides (1) don't use IE11 or (2) don't use Function.name.

Sample:

// Typescript
class Animal {    
    hello(person: string) {
        console.log(`Hello ${person}`);
    }
}

class Snake extends Animal {    
    hello(person: string) {
        console.log("I'm a snake");
        super.hello(person);
    }
}

class Horse extends Animal {
    get wow(): string {
        return "wow";
    }
}

console.log(Animal.name);
console.log(Snake.name);
console.log(Horse.name);

tsc -t ES2015 with produces this JavaScript:

class Animal {
    hello(person) {
        console.log(`Hello ${person}`);
    }
}
class Snake extends Animal {
    hello(person) {
        console.log("I'm a snake");
        super.hello(person);
    }
}
class Horse extends Animal {
    get wow() {
        return "wow";
    }
}
console.log(Animal.name);
console.log(Snake.name);
console.log(Horse.name);

Then babel with "presets": ["es2015"] generates the es5 Javascript:

"use strict";

var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Animal = function () {
    function Animal() {
        _classCallCheck(this, Animal);
    }

    _createClass(Animal, [{
        key: "hello",
        value: function hello(person) {
            console.log("Hello " + person);
        }
    }]);

    return Animal;
}();

var Snake = function (_Animal) {
    _inherits(Snake, _Animal);

    function Snake() {
        _classCallCheck(this, Snake);

        return _possibleConstructorReturn(this, (Snake.__proto__ || Object.getPrototypeOf(Snake)).apply(this, arguments));
    }

    _createClass(Snake, [{
        key: "hello",
        value: function hello(person) {
            console.log("I'm a snake");
            _get(Snake.prototype.__proto__ || Object.getPrototypeOf(Snake.prototype), "hello", this).call(this, person);
        }
    }]);

    return Snake;
}(Animal);

var Horse = function (_Animal2) {
    _inherits(Horse, _Animal2);

    function Horse() {
        _classCallCheck(this, Horse);

        return _possibleConstructorReturn(this, (Horse.__proto__ || Object.getPrototypeOf(Horse)).apply(this, arguments));
    }

    _createClass(Horse, [{
        key: "wow",
        get: function get() {
            return "wow";
        }
    }]);

    return Horse;
}(Animal);

console.log(Animal.name);
console.log(Snake.name); // returns Animal in IE11
console.log(Horse.name); // returns Animal in IE11

<!DOCTYPE html>
<html>    
    <head> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
        <script src="node_modules/babel-polyfill/dist/polyfill.js"></script>
        <script src="sample.es5.js"></script>
    </head>
</html>

Upvotes: 2

Views: 1043

Answers (1)

Mike Taj
Mike Taj

Reputation: 21

I paid the $$$ and opened a support ticket with Microsoft. This is a known bug with IE11, and may be fixed in the next release.

Upvotes: 0

Related Questions