Quiero hacer una biblioteca js y tengo todo el código. Las bibliotecas como Jquery pueden permitirte hacer cosas como
<script src = "..."></script> <script> //uses the library </script> sin la import * from JQuery . ¿Cómo puedo hacer esto sin declarar la biblioteca modular o importar?
Asigne el objeto de nivel superior a una propiedad en window , tal como lo hace jQuery:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> // testing jQuery: console.log(window.$); console.log(window.jQuery); console.log($); // same as window.$ console.log(window.jQuery === $); </script> <script> // your module (imagine this was hosted and loaded via the "src" attribute like jQuery) const myMath = {}; myMath.add = (...numbers) => numbers.reduce((sum, n) => sum + n, 0); window.MY_MATH = myMath; </script> <script> // use myMath const {add} = window.MY_MATH; console.log(add(1, 2, 3, 4)); // 10 </script>