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 Supersedes: <2573021.mvXUDI8C0e@PointedEars.de> Date: Mon, 22 Mar 2021 21:05:28 +0100 Organization: PointedEars Software (PES) Lines: 56 Message-ID: <5445729.DvuYhMxLoT@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="27590"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.10 Cancel-Key: sha1:7Kez12TBtgFew62Xk16i4QZPzcI= Cancel-Lock: sha1:7GrN33WV1VfEbH0S26Xa5FgOBwc= X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. It looks like in the system for which I need to write > server-side JavaScript, neither CommonJS nor ECMAScript > modules are supported, but it provides its own module system > that works as shown below: > > $.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"), I think you mean the _local execution context_ instead, as the current scope is somewhat loosely defined as it extends to the global execution context. Modules were introduced precisely to avoid the problems that your wish typically produces (what if there is a log() etc. defined in the execution context already?). > rather than, say, util.log("Hello"), While there is at least one fancy way to do this for several and selected properties only (which a custom module system might provide), the simple code const util = $.anton_test.util; (or var util = $.anton_test.util; to be backwards-compatible), is literally the best that you can do, in general. Note that const log = $.anton_test.util.log; is NOT equivalent because “$.anton_test.util.log()” may use “this” to refer to “$.anton_test.util” which will refer to the global object, or (in strict mode) be the “undefined” value, if you then call “log()”. The equivalent variant would be const log = $.anton_test.util.log.bind($.anton_test.util); but when emulated for backwards compatibility (and maybe even when Function.prototype.bind() is available as a built-in), this comes at the price of reduced runtime efficiency when it is is called. So just to be able to write “log” instead of “util.log” hardly appears to be worth it. -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.