Structuring Javascript code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom de Neef

    Structuring Javascript code

    I hope you don't mind trivial questions. I only ask them after failing to
    find answers in my book and on the web...

    Can I split Javascript code into separate js files which reference each
    other (hierarchically )? For instance, can I put core functions in a library
    file and refer to them from other script files (like: $ïnclude "core.js").

    TIA
    Tom


  • Jeremy J Starcher

    #2
    Re: Structuring Javascript code

    On Wed, 20 Feb 2008 17:40:11 +0100, Tom de Neef wrote:
    Can I split Javascript code into separate js files which reference each
    other (hierarchically )? For instance, can I put core functions in a
    library file and refer to them from other script files (like: $nclude
    "core.js").
    You can separate javascript into separate files that are read in with the
    <scripttag. You need one tag per javascript file to include.

    <script src="core.js" type="text/javascript"></script>

    (There are some tricks make one javascript file to include the others, but
    that is often regarded as 'dirty' at best and unreliable at worst.)

    Comment

    • Peter Michaux

      #3
      Re: Structuring Javascript code

      On Feb 20, 8:40 am, "Tom de Neef" <tden...@qolor. nlwrote:
      I hope you don't mind trivial questions. I only ask them after failing to
      find answers in my book and on the web...
      >
      Can I split Javascript code into separate js files which reference each
      other (hierarchically )? For instance, can I put core functions in a library
      file and refer to them from other script files (like: $ïnclude "core.js").
      There are quite a few "module loader" systems out there that allow
      building dependencies like this. I don't use such a system. I just
      manually list them in the page like this

      <script type="text/javascript src="core.js"></script>
      <script type="text/javascript src="app.js"></script>

      I think the module loaders encourage making many HTTP requests to the
      server and that is a bad idea. It is better to have a build system
      that aggregates the various JavaScript development files into
      production files with the whitespace removed etc.

      Peter

      Comment

      • Tom de Neef

        #4
        Re: Structuring Javascript code

        "Peter Michaux" <petermichaux@g mail.comschreef in bericht
        news:0bf33c4e-a59f-4e53-8ff1-b1af1cdf6c57@o1 0g2000hsf.googl egroups.com...
        On Feb 20, 8:40 am, "Tom de Neef" <tden...@qolor. nlwrote:
        I hope you don't mind trivial questions. I only ask them after failing to
        find answers in my book and on the web...
        >
        Can I split Javascript code into separate js files which reference each
        other (hierarchically )? For instance, can I put core functions in a
        library
        file and refer to them from other script files (like: $ïnclude "core.js").

        <script type="text/javascript src="core.js"></script>
        <script type="text/javascript src="app.js"></script>

        So, the HTML will bind it all together. That is fine: a function defined in
        "core.js" can be referenced in "app.js" and within the HTML it will work
        together.

        That does not solve the problem of how to test "app.js" without the HTML, if
        it calls a function declared in "core.js". Or do you never test the script
        standalone? In my editor (Antechinus) standalone testing is an option.
        Tom


        Comment

        • Peter Michaux

          #5
          Re: Structuring Javascript code

          On Feb 20, 10:43 am, "Tom de Neef" <tden...@qolor. nlwrote:
          "Peter Michaux" <petermich...@g mail.comschreef in berichtnews:0bf 33c4e-a59f-4e53-8ff1-b1af1cdf6c57@o1 0g2000hsf.googl egroups.com...
          On Feb 20, 8:40 am, "Tom de Neef" <tden...@qolor. nlwrote:
          >
          I hope you don't mind trivial questions. I only ask them after failing to
          find answers in my book and on the web...
          >
          Can I split Javascript code into separate js files which reference each
          other (hierarchically )? For instance, can I put core functions in a
          library
          file and refer to them from other script files (like: $ïnclude "core.js").
          >
          <script type="text/javascript src="core.js"></script>
          <script type="text/javascript src="app.js"></script>
          >
          So, the HTML will bind it all together. That is fine: a function defined in
          "core.js" can be referenced in "app.js" and within the HTML it will work
          together.
          You are correct. All the scripts are evaluated in the same global
          namespace.
          That does not solve the problem of how to test "app.js" without the HTML, if
          it calls a function declared in "core.js". Or do you never test the script
          standalone? In my editor (Antechinus) standalone testing is an option.
          I suppose the editor should be able to be told which scripts to "load"
          when testing a particular script.

          Peter

          Comment

          Working...