所以我最近开始学习如何使用 babel 转译器,但我发现了一个与预设的主要区别。当我使用 es2015 预设时。代码已成功转译,并且在我的浏览器中运行良好,但是当我使用 env-preset 时,代码也已转译,但根本不起作用。
下面是我用ES6写的原代码
const jokeEl = document.getElementById('joke');
const getJoke = document.getElementById('getJoke');
getJoke.addEventListener('click', generateJoke);
generateJoke();
async function generateJoke () {
const jokeRes = await fetch('https://icanhazdadjoke.com/', {
headers: {
'Accept' : 'application/json'
}
});
const joke = await jokeRes.json();
jokeEl.textContent = joke.joke;
}
这是我使用 es2015 预设时有效的代码。
'use strict';
var jokeEl = document.getElementById('joke');
var getJoke = document.getElementById('getJoke');
getJoke.addEventListener('click', generateJoke);
generateJoke();
async function generateJoke () {
var jokeRes = await fetch('https://icanhazdadjoke.com/', {
headers: {
'Accept' : 'application/json'
}
});
var joke = await jokeRes.json();
jokeEl.textContent = joke.joke;
}
然后,这是不起作用的代码,它是使用 env 预设生成的。
"use strict";
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
var jokeEl = document.getElementById('joke');
var getJoke = document.getElementById('getJoke');
getJoke.addEventListener('click', generateJoke);
generateJoke();
function generateJoke() {
return _generateJoke.apply(this, arguments);
}
呼如林
相关分类