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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745268067a4619578.html

相关推荐

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

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

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信