首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多填充IE11 Function.name的Javascript行为

多填充IE11 Function.name的Javascript行为
EN

Stack Overflow用户
提问于 2017-02-26 15:11:09
回答 1查看 1K关注 0票数 2

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

如果按基类->子类的顺序调用Function.name,那么类上的调用就会失败。如果按子->基类顺序调用,则成功。

这个例子:

代码语言:javascript
复制
console.log(Animal.name);
console.log(Snake.name); // returns Animal in IE11
console.log(Horse.name); // returns Animal in IE11

然而:

代码语言:javascript
复制
console.log(Snake.name); // returns Snake in IE11
console.log(Horse.name); // returns Horse in IE11
console.log(Animal.name);

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

我有一个简单的类层次结构示例,它是我使用类型记录构建的,它被显示在es6中,然后被转换到es5中。

示例页面包括babel多边形填充和脚本。

我也试过JamesMGreene/Function.name polyfill。

基于代码的遍历,我认为这不是问题所在。这似乎是IE11 JIT中的一个缺陷。

除了(1)不要使用IE11或(2)不要使用Function.name之外,我还在寻找关于如何处理这个问题的任何建议。

示例:

代码语言:javascript
复制
// 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:

代码语言: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);

然后使用“预设”:"es2015“生成es5 Javascript:

代码语言: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

代码语言:javascript
复制
<!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>

EN

回答 1

Stack Overflow用户

发布于 2017-03-25 22:35:03

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42470179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档