0

Hello I playing with javascript and I have the following situation

I have defined a point class. I have defined an edge class which depends on point and a triangle class which depends both on point and edge. How do I ensure that those classes are loaded correctly?

I use them as follows:

<script src="point.js" type="text/javascript"></script>
<script src="edge.js" type="text/javascript"></script>
<script src="triangle.js" type="text/javascript"></script>
<script src="utils.js" type="text/javascript"></script>

the utils.js file are some generic utility functions that uses all of the above classes. My problem is that I cannot tell the triangle class for example, that the point and edge classes are defined. My code just assumes them to be defined somewhere or else it crashes burning down. Is there a simple #include style mechanism for javascript?

For example I would like to just use the triangle class which internally should somehow find about its dependencies and load them automagically

msmechanized
  • 436
  • 4
  • 19

1 Answers1

2

For example I would like to just use the triangle class which internally should somehow find about its dependencies and load them automagically

You're in luck! Tools like RequireJS exist specifically for this purpose, and do it very well. Take a look at RequireJS's Get Started docs and the many available tutorials. There are other, similar tools like Browserify, but RequireJS is a good place to start and has lots of documentation and users.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • 1
    Wow this sucks. I cannot believe that javascript allows you to have objects but does not allow you to define dependencies between them. Anyways, this solves the problem == accepted. – msmechanized Aug 05 '14 at 15:51
  • If you were around when JavaScript debuted in 1996 you would not be surprised at all. Happily, ES6 will have module support baked in. You can [read about it here](http://eviltrout.com/2014/05/03/getting-started-with-es6.html), which includes links on how to use the draft syntax (with a build script) today. – Jordan Running Aug 05 '14 at 15:56