【问题标题】:Transpiling ES6 to ES5 online with Babel使用 Babel 在线将 ES6 转译为 ES5
【发布时间】:2017-06-15 01:35:37
【问题描述】:

如您所见here 我想将以下代码从 ES6 转换为 ES5 并用作预设“es2015”:

let myString = "Whatever";
let myStringArray = Array.from (myString);
console.log (myStringArray);

如您所知,ES5 中没有“Array.from”方法,但转译后的代码仍然包含Array.from。我做错了什么?

【问题讨论】:

  • babeljs.io/docs/usage/polyfill。你没有做错任何事,Babel 不会转译 API。
  • 谢谢,但我正在 babel 游乐场在线尝试,我想把它转译在那里,..
  • 我更新了关于 Babel 不会转译内置 API 的评论。据我所知,您在在线工具中无能为力。
  • 不可能在转译器中处理Array.from 的所有情况,这就是存在polyfills 的原因。你需要做的就是像上面的链接那样加载一个polyfill。

标签: javascript ecmascript-6 babeljs transpiler


【解决方案1】:

在您的代码上方包含此polyfill snippet,以便您可以在线即时对其进行转译。但是对于生产你应该使用babel-polyfill

// Production steps of ECMA-262, Edition 6, 22.1.2.1
// Reference: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from
if (!Array.from) {
  Array.from = (function () {
    var toStr = Object.prototype.toString;
    var isCallable = function (fn) {
      return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
    };
    var toInteger = function (value) {
      var number = Number(value);
      if (isNaN(number)) { return 0; }
      if (number === 0 || !isFinite(number)) { return number; }
      return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
    };
    var maxSafeInteger = Math.pow(2, 53) - 1;
    var toLength = function (value) {
      var len = toInteger(value);
      return Math.min(Math.max(len, 0), maxSafeInteger);
    };

    // The length property of the from method is 1.
    return function from(arrayLike/*, mapFn, thisArg */) {
      // 1. Let C be the this value.
      var C = this;

      // 2. Let items be ToObject(arrayLike).
      var items = Object(arrayLike);

      // 3. ReturnIfAbrupt(items).
      if (arrayLike == null) {
        throw new TypeError("Array.from requires an array-like object - not null or undefined");
      }

      // 4. If mapfn is undefined, then let mapping be false.
      var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
      var T;
      if (typeof mapFn !== 'undefined') {
        // 5. else      
        // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
        if (!isCallable(mapFn)) {
          throw new TypeError('Array.from: when provided, the second argument must be a function');
        }

        // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
        if (arguments.length > 2) {
          T = arguments[2];
        }
      }

      // 10. Let lenValue be Get(items, "length").
      // 11. Let len be ToLength(lenValue).
      var len = toLength(items.length);

      // 13. If IsConstructor(C) is true, then
      // 13. a. Let A be the result of calling the [[Construct]] internal method of C with an argument list containing the single item len.
      // 14. a. Else, Let A be ArrayCreate(len).
      var A = isCallable(C) ? Object(new C(len)) : new Array(len);

      // 16. Let k be 0.
      var k = 0;
      // 17. Repeat, while k < len… (also steps a - h)
      var kValue;
      while (k < len) {
        kValue = items[k];
        if (mapFn) {
          A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
        } else {
          A[k] = kValue;
        }
        k += 1;
      }
      // 18. Let putStatus be Put(A, "length", len, true).
      A.length = len;
      // 20. Return A.
      return A;
    };
  }());
}

【讨论】:

  • 那是不真实的,从 TypeScript 编译的 JavaScript 有更多的开销,如果你使用 JavaScript 不支持的 TypeScript 功能,必须“模拟”
  • 某些功能除外,例如ES6 模块,现代浏览器已经支持 ES6...
猜你喜欢
  • 2017-08-07
  • 2019-03-21
  • 2020-05-29
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多