使用polyfilled Function.name的IE11 Javascript行为

时间:2017-02-26 15:11:09

标签: javascript typescript ecmascript-6 internet-explorer-11 babel

我在IE11 JavaScript JIT中遇到了一个奇怪的行为。

如果按类base-> child的顺序调用,则调用Classes上的Function.name失败。如果在child->基类顺序中调用,则成功。

这个例子:

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

然而:

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

注意:在此示例中,更改子项的顺序没有任何效果。

我有一个简单的类层次结构示例,我使用Typescript构建,然后将其发送到es6,然后将其转换为es5。

示例页面包含babel polyfill和脚本。

我也尝试过JamesMGreene / Function.name polyfill。

基于行走代码我不认为这是问题。这似乎是IE11 JIT中的一个缺陷。

我正在寻找有关如何处理此问题的任何建议(1)不使用IE11或(2)不使用Function.name。

样品:

// 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与生成此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);

然后babel用“presets”:[“es2015”]生成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>

1 个答案:

答案 0 :(得分:0)

我支付了$$$并向微软开了一张支持票。这是IE11的一个已知错误,可以在 next 版本中修复。