पाठ 16 / 30
आधुनिक Features को Transpile करना
जानें कि async/await, classes, और destructuring transpilation के बाद क्या बनते हैं।
Arrow functions को function expressions में
Arrow functions सुविधाजनक हैं, लेकिन पुराने browsers इन्हें support नहीं करते। Babel उन्हें regular functions में convert करता है।
// Input (ES6)
const square = (x) => x * x;
const greet = name => `Hello, ${name}`;
// Output (ES5)
var square = function(x) {
return x * x;
};
var greet = function(name) {
return "Hello, " + name;
};Async/await को promises में
Async/await promises के लिए syntactic sugar है। Babel async () => { await ... } को promise chains में convert करता है।
// Input (async/await)
async function fetchData() {
const data = await fetch('/api');
return data.json();
}
// Output (promise-based)
function fetchData() {
return __awaiter(this, void 0, void 0, function*() {
const data = yield fetch('/api');
return data.json();
});
}Syntax बनाम runtime features
Babel syntax को transpile कर सकता है (arrow functions, destructuring) लेकिन runtime features को नहीं जोड़ सकता जो exist नहीं करते (Array.prototype.at())। Runtime features के लिए polyfills का उपयोग करें।