Path: csiph.com!3.eu.feeder.erje.net!feeder.erje.net!news.mb-net.net!open-news-network.org!.POSTED.178.197.213.187!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Exposing a module in the current scope Date: Tue, 23 Mar 2021 01:00:34 +0100 Organization: PointedEars Software (PES) Lines: 52 Message-ID: <1777535.tdWV9SEqCh@PointedEars.de> References: <20210318192107.08e2fac2d6aeada5596c4dd0@freeshell.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit Injection-Info: gwaiyur.mb-net.net; posting-host="178.197.213.187"; logging-data="24838"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.10 Cancel-Lock: sha1:zenE0YOYDN8//AOkzbr38oDodJA= X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. $.import("anton_test/util.xsjslib","util"); // import module > $.anton_test.util.log("Log from library."); // invoke a function > > Is it somehow possible to have all the functions from `util' > available in the current scope, so that I could invoke them > by their names: log("Hello"), rather than, say, > util.log("Hello"), (Again: No.) But an alternative standard-compliant and backwards-compatible way is what has been called an IEFE (“immediately executed function expression”) or a simiilar term: $.import("anton_test/util.xsjslib","util"); // import module $.anton_test.util.log("Log from library."); // invoke a function (function (log) { /* … */ }($.anton_test.util.log)); The same caveat applies above as for const log = $.anton_test.util.log; which is why typically one writes (function (util) { util.log("Log from library."); }($.anton_test.util)); BTW, I would expect the module loader of such a module system to return, if successful, a reference to the module object such that – if synchronous execution cannot be avoided – the following code would be equivalent to the code above: const util = $.import("anton_test/util.xsjslib","util"); util.log("Log from library."); In asynchronous module loading, typically the module object is passed as argument to the callback: $.import("anton_test/util.xsjslib", function (util) { util.log("Log from library."); }); -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.