import as हमें एक नेम्ड मॉड्यूल को अलग नाम से इम्पोर्ट करने की अनुमति देता है।
export as हमें एक नेम्ड मॉड्यूल को अलग नाम से एक्सपोर्ट करने की अनुमति देता है।
जावास्क्रिप्ट में import as और export as कंस्ट्रक्ट के लिए कोड निम्नलिखित है:
उदाहरण
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } </style> </head> <body> <h1>Import as and Export as in JavaScript</h1> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to execute the imported function</h3> <script src="script.js" type="module"></script> </body> </html>
script.js
import {test,tellTime as showTime} from "./sample.js"; let resultEle = document.querySelector('.result'); document.querySelector('.Btn').addEventListener('click',()=>{ resultEle.innerHTML+=test(); resultEle.innerHTML+=showTime(); })
sample.js
function testImport() { return "Module testImport has been imported" + ""; } function tellTime() { return new Date(); } export { testImport as test, tellTime };
आउटपुट
उपरोक्त कोड निम्नलिखित आउटपुट उत्पन्न करेगा:

'CLICK HERE' बटन पर क्लिक करने पर:
