Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Why is it so convoluted to import a .js file in javascript? Date: Thu, 27 Mar 2014 22:29:03 +0100 Organization: PointedEars Software (PES) Lines: 88 Message-ID: <5419492.dk8GnlDV4K@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1395955745 19020 eJwFwYEBwCAMArCX1iG4vlOU/09YQqjkvUQthjlTELYbVsaR4rnWWxhs+kOO6Nat9PTzAy5TEaM= (27 Mar 2014 21:29:05 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Thu, 27 Mar 2014 21:29:05 +0000 (UTC) User-Agent: KNode/4.11.5 X-User-ID: eJwNyckBwCAIBMCWxJVFykGO/ktI5jsKCtMOlUdHxx5vQOAj1t0126QOqpDbYM9VH+qmc7UGnNn/2eIOj058WOoV0Q== Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. jonas.thornvall@gmail.com writes: >> // load math.js and create an instance >> var mathjs = require('math,js'), > > s/,/./ > > The method »require« is a method of the modules API of > Node.js, ACK. > it is not a part of standard JavaScript AFAIK. There is no “standard JavaScript”; the various implementations/languages that call themselves „JavaScript“ (most notably: JavaScript™, JScript, V8, Opera ECMAScript, and KJS) are *implementations of* a(n Ecma) standard, the ECMAScript Language Specification (ECMA-262). Conforming implementations of ECMAScript *may extend* the Specification’s algorithms and syntax: This function is not specified in ECMAScript (yet?), and AFAIK it is not available in any „JavaScript“. In Node.js, it is a feature provided by the host environment that runs V8 JavaScript: >>Why can't a simple >>import('../test/test.js'); or append('../test/test.js'); >>do? > > When JavaScript is running in a web browser, scripts can be > loaded in HTML using > > “script” elements can also be inserted dynamically, causing the script code to be loaded, which is closest to Node.js's approach. I do not think this is limited to HTML. However, for this to work reliably, the host environment must support either a way to determine when the script resource has been loaded (and parsed) – this is available in a so far conforming implementation of W3C DOM Level 3 Events (WD) [1] – or synchronous XHR (which allows the normal program flow to continue only after the resource has been loaded; the usual caveats apply). An example for the former is: function require (source, callback) { var script = document.createElement("script"); script.addEventListener("load", callback); script.src = source; /* HTML5/WHATWG HTML only */ document.head.appendChild(script); } require("http://script.example/foo.js", function () { console.log("Script loaded"); }); (There are libraries that implement and improve this or a similar approach; so far none can be recommended, none can be recommended against. Use it and them with caution.) A proof of concept for the latter is provided by jsx.importFrom() and jsx.importOnce() in JSX:object.js [2] > . JavaScript does not have a means to import a file itself. > Maybe because it does not want to include features specific > to only some environments, such as filesystem access. ACK. ________ [1] [2] (Note that jsx.require() does _not_ implement the asynchronous approach yet.) -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.