Re: Javascript: Time Traveller From the Year 1962!

[prev] [thread] [next] [lurker] [Date index for 2005/04/09]

From: Michael G Schwern
Subject: Re: Javascript: Time Traveller From the Year 1962!
Date: 23:49 on 09 Apr 2005
On Sat, Apr 09, 2005 at 07:45:36AM -0500, Peter da Silva wrote:
> > Oh for fuck's sake, all I want to be able to do is load other .js files 
> > so that I can modularize things and appropriately manage dependencies. 
> 
> That's not something I would even expect to be defined in the language,
> any more than "#include <stdio.h>" is defined in C.

<snip>

> I would expect you'd do something like this:
> 
> <script source="includefile.js"></>
> <script source="anotherinclude.js"></>
> <script source="code.js"></>

Ahh, the fundamental difference here is that the include in C goes into the
source code but the include in JS goes into the thing USING the source code.

Major difference.

Let's say we have a library Foo which depends on libraries Lib1 and Lib2.  In
most programming languages it would be written like this.

	# The library Foo
	include Lib1;
	include Lib2;

	function Foo {
		... uses stuff from Lib1 and Lib2 ...
	}


	# The program which uses Foo
	include Foo;

	Foo(...);

Foo's dependencies on Lib1 and Lib2 are resolved inside Foo encapsulating
it from the user.

But since Javascript has no include() of its own it falls upon the *user of
the code* to resolve the dependencies.  The code is written like this:

	# The library Foo
	function Foo {
		... uses stuff from Lib1 and Lib2 ...
	}


	# The program which uses Foo
	include Lib1;
	include Lib2;
	include Foo;

	Foo(...);

Now internal information about Foo has leaked out into the user world.
Encapsuation is violated.  If I want to change Foo's internal dependencies
I must now find all the code which uses Foo and change it.

This is Bad.  This discourages modular programming.  This leaves everyone
to reimplement basic functionality over and over again.  That's the hate
I'm talking about.  Anything that involves putting the include functionality
into the web page, be it <script> or SSI or whatever, has that hate.


C gets away with pasting together its source code because it is a static
language with distinct compile, link and run stages (even still dynamic
loading was bolted on).  Javascript is not.  Its an interpreted/JIT language 
which does not have a build step in which to run a preprocessor.  You write 
the code, it runs.  Adding a build step for a preprocessor would be clunky
and artificial.


> I can't conceptually see a better option, really, than something like:
>
> document.write("<script source=\"include.js\"></script>");

I can.  The above idiom relies on the existence of a DOM beyond the pure
EMCAscript language.  The DOM defines hundreds of built in functions.
Well for god's sake, could they stop adding new ways to make things rotate
for a second and add one useful function to the language?!

	function include(source) {
		document.write("<script source\"" + source + "\"></script>");
	}

At least then I don't have to cut & paste that code into every .js file
I write.  Sheesh.

There's stuff above here

Generated at 12:00 on 12 Apr 2005 by mariachi 0.52