最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Polyfill (kindof) for const? - Stack Overflow

programmeradmin0浏览0评论

Actually I am dealing with the problem to let my code work in IE. Unfortunately this is still necessary.

Polyfills for unsupported methods are working fine. But I wonder, how / if I can make "const" and "let" work. Is there any way to do that?

Same question about arrow-functions which may be much harder.

Best regards, Christian

Actually I am dealing with the problem to let my code work in IE. Unfortunately this is still necessary.

Polyfills for unsupported methods are working fine. But I wonder, how / if I can make "const" and "let" work. Is there any way to do that?

Same question about arrow-functions which may be much harder.

Best regards, Christian

Share Improve this question asked Feb 5, 2018 at 13:19 Christian HeischChristian Heisch 2451 gold badge3 silver badges10 bronze badges 3
  • 3 I don't think you can create polyfil for keywords or reserved words. – gurvinder372 Commented Feb 5, 2018 at 13:20
  • 1 You may find it better to transpile your code, if possible. – Federico klez Culloca Commented Feb 5, 2018 at 13:21
  • 1 No, if you have to support IE, than you need to use something like babel in your build process. – epascarello Commented Feb 5, 2018 at 13:22
Add a ment  | 

1 Answer 1

Reset to default 6

You can't polyfill syntax changes to the language. But you can transpile your code that uses new syntax into code that uses older syntax, using tools like Babel or Traceur, and then run that generated older-style code on IE. They pile (or "transpile") code written with, say, ES2015+ features into code written with only ES5-level features (usually bined with polyfills).

For example, this code using an arrow function:

Nifty.prototype.setupHandler = function() {
    this.element.addEventHandler("click", e => {
        ++this.clicks;
    });
};

is transpiled by Babel into:

Nifty.prototype.setupHandler = function () {
    var _this = this;

    this.element.addEventHandler("click", function (e) {
        ++_this.clicks;
    });
};

Example

发布评论

评论列表(0)

  1. 暂无评论