通过Babel在线将ES6转换为ES5

Lon*_*ely 5 javascript transpiler ecmascript-6 babeljs

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

let myString = "Whatever";
let myStringArray = Array.from (myString);
console.log (myStringArray);
Run Code Online (Sandbox Code Playgroud)

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

Non*_*ial 4

将此填充代码片段包含在您的代码上方,以便您可以在线即时转译它。但对于生产你应该使用babel-polyfill

\n\n
// Production steps of ECMA-262, Edition 6, 22.1.2.1\n// Reference: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from\nif (!Array.from) {\n  Array.from = (function () {\n    var toStr = Object.prototype.toString;\n    var isCallable = function (fn) {\n      return typeof fn === \'function\' || toStr.call(fn) === \'[object Function]\';\n    };\n    var toInteger = function (value) {\n      var number = Number(value);\n      if (isNaN(number)) { return 0; }\n      if (number === 0 || !isFinite(number)) { return number; }\n      return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));\n    };\n    var maxSafeInteger = Math.pow(2, 53) - 1;\n    var toLength = function (value) {\n      var len = toInteger(value);\n      return Math.min(Math.max(len, 0), maxSafeInteger);\n    };\n\n    // The length property of the from method is 1.\n    return function from(arrayLike/*, mapFn, thisArg */) {\n      // 1. Let C be the this value.\n      var C = this;\n\n      // 2. Let items be ToObject(arrayLike).\n      var items = Object(arrayLike);\n\n      // 3. ReturnIfAbrupt(items).\n      if (arrayLike == null) {\n        throw new TypeError("Array.from requires an array-like object - not null or undefined");\n      }\n\n      // 4. If mapfn is undefined, then let mapping be false.\n      var mapFn = arguments.length > 1 ? arguments[1] : void undefined;\n      var T;\n      if (typeof mapFn !== \'undefined\') {\n        // 5. else      \n        // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.\n        if (!isCallable(mapFn)) {\n          throw new TypeError(\'Array.from: when provided, the second argument must be a function\');\n        }\n\n        // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.\n        if (arguments.length > 2) {\n          T = arguments[2];\n        }\n      }\n\n      // 10. Let lenValue be Get(items, "length").\n      // 11. Let len be ToLength(lenValue).\n      var len = toLength(items.length);\n\n      // 13. If IsConstructor(C) is true, then\n      // 13. a. Let A be the result of calling the [[Construct]] internal method of C with an argument list containing the single item len.\n      // 14. a. Else, Let A be ArrayCreate(len).\n      var A = isCallable(C) ? Object(new C(len)) : new Array(len);\n\n      // 16. Let k be 0.\n      var k = 0;\n      // 17. Repeat, while k < len\xe2\x80\xa6 (also steps a - h)\n      var kValue;\n      while (k < len) {\n        kValue = items[k];\n        if (mapFn) {\n          A[k] = typeof T === \'undefined\' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);\n        } else {\n          A[k] = kValue;\n        }\n        k += 1;\n      }\n      // 18. Let putStatus be Put(A, "length", len, true).\n      A.length = len;\n      // 20. Return A.\n      return A;\n    };\n  }());\n}\n
Run Code Online (Sandbox Code Playgroud)\n