【问题标题】:IE11 Javascript behavior with polyfilled Function.name带有 polyfill Function.name 的 IE11 Javascript 行为
【发布时间】:2017-07-17 03:32:01
【问题描述】:

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

如果按照基类->子类的顺序调用,则在类上调用 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,然后被 babel'd 转换为 es5。

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

我也尝试过 JamesMGreene/Function.name 填充。

基于遍历代码,我认为这不是问题。这似乎是 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 with 产生这个 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>

【问题讨论】:

  • (3) 将其作为错误报告给 MS。

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


【解决方案1】:

我支付了 $$$ 并与 Microsoft 开了一张支持票。这是 IE11 的一个已知错误,可能会在下一个版本中修复。

【讨论】:

    猜你喜欢
    • 2020-03-23
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 2021-03-12
    • 2017-12-30
    相关资源
    最近更新 更多