Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #23713

Re: Why is it so convoluted to import a .js file in javascript?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
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> (permalink)
References <d1050ce2-5b06-4bb8-9d69-18cc325b30c0@googlegroups.com> <include-20140327003813@ram.dialup.fu-berlin.de>
Reply-To Thomas 'PointedEars' Lahn <usenet@PointedEars.de>
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+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Cancel-Lock sha1:raEitBvzQoaF7w1C7a2hLxYkbMI=
X-NNTP-Posting-Host eJwFwQkBwCAMA0BLhPSbHGiofwm7cwai08LDfHw21r5oC7aA9cSmCmIdAKaPmjTw0lOo/AELXxBU
Xref csiph.com comp.lang.javascript:23713

Show key headers only | View raw


Stefan Ram wrote:

> 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:

<http://PointedEars.de/es-matrix>

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:

<http://nodejs.org/api/modules.html>
 
>>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 type="text/javascript" src="../test/test.js"></script>

“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] <http://www.w3.org/TR/DOM-Level-3-Events/#event-type-load>
[2] <http://PointedEars.de/wsvn/JSX/trunk/object.js>
    (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.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-26 16:22 -0700
  Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-27 01:37 -0700
    Re: Why is it so convoluted to import a .js file in javascript? Tim Streater <timstreater@greenbee.net> - 2014-03-27 11:45 +0000
      Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-27 09:36 -0700
        Re: Why is it so convoluted to import a .js file in javascript? Tim Streater <timstreater@greenbee.net> - 2014-03-27 17:17 +0000
        Re: Why is it so convoluted to import a .js file in javascript? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-27 19:48 +0100
          Re: Why is it so convoluted to import a .js file in javascript? "se" <se@no-one.here> - 2014-03-27 20:52 +0100
          Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:39 -0700
          Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:59 -0700
            Re: Why is it so convoluted to import a .js file in javascript? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-28 18:19 +0100
              Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 10:41 -0700
                eval() vs. own expression parser (was: Why is it so convoluted to import a .js file in javascript?) Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-28 19:11 +0100
                Re: Why is it so convoluted to import a .js file in javascript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-28 21:43 +0100
                Re: Why is it so convoluted to import a .js file in javascript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-29 18:57 +0100
      Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:51 -0700
    Re: Why is it so convoluted to import a .js file in javascript? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2014-03-27 17:30 +0100
  Re: Why is it so convoluted to import a .js file in javascript? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-27 09:39 +0100
  Re: Why is it so convoluted to import a .js file in javascript? "se" <se@no-one.here> - 2014-03-27 21:12 +0100
    Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 07:11 -0700
      Re: Why is it so convoluted to import a .js file in javascript? John Harris <niam@jghnorth.org.uk.invalid> - 2014-03-28 14:30 +0000
      Re: Why is it so convoluted to import a .js file in javascript? danca <cyberdanny@tiscalinet.it> - 2014-03-28 15:36 +0100
        Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:17 -0700
          Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 08:23 -0700
            Re: Why is it so convoluted to import a .js file in javascript? danca <cyberdanny@tiscalinet.it> - 2014-03-29 16:42 +0100
    Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 09:02 -0700
      Re: Why is it so convoluted to import a .js file in javascript? "se" <se@no-one.here> - 2014-03-28 19:00 +0100
        Re: Why is it so convoluted to import a .js file in javascript? jonas.thornvall@gmail.com - 2014-03-28 11:34 -0700
  Re: Why is it so convoluted to import a .js file in javascript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-27 22:29 +0100

csiph-web