2

I am trying to use Animal object inside the World object but both of them are in different files.

Animal.js :

function Animal(species, nature) {

  // Public Fields
  this.species = species;
  this.nature = nature;

  // Private Fields
  var preys = new Array();

  // GET Functions
  ..........
  ..........

  // SET Functions
  ..........
  ..........  
}

World.js :

function World() {

  var animals = new Array();

  var animal = new Animal("Wolf", "Carnivore");    //[1]

  animals.push(new Animal("Wolf", "Carnivore"));   //[2]
}

I am calling word class in a basic website which contains nothing but javaScript code which creates a new World instance. Then in the browser, I press F12 and both in step 1 and step 2 I am getting an error.

Error ScreenShot : [Error image]

I think it can not find the animal class. I read a number of articles and also searched on the internet but I could not find(Maybe because of being new in JavaScript).

EDIT :

In other words, I am trying to import Animal.js file into the World.js file. Then I will import World.js to Ecosystem.js

Folders in my project

And here is the asp.net folder :

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="scripts/World.js"></script>
    <script  type="text/javascript">
        world = new World();
        world.getOmnivores();
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
</body>
</html>
Tomer
  • 17,787
  • 15
  • 78
  • 137
Ahmet Batu Orhan
  • 118
  • 2
  • 14
  • Possible duplicate of [Include a JavaScript file in another JavaScript file?](http://stackoverflow.com/questions/950087/include-a-javascript-file-in-another-javascript-file) – Takoyaro Jun 14 '16 at 12:46
  • Have a look at require.js to start with... – Sten Muchow Jun 14 '16 at 12:46
  • Or [ES6 Modules](http://exploringjs.com/es6/ch_modules.html) – mferly Jun 14 '16 at 12:48
  • 4
    Exactly how are these files being included in the site? Global symbols in separate files become global symbols in the browser context, so unless there's something else going on that you have not posted, what you're doing should work. This is a simple situation, and things like require.js or ES6 modules are completely pointless here. – Pointy Jun 14 '16 at 12:50
  • @GaijinJim I am not using an HTML code or AJAX or JQuery. I just have two different JavaScript Files. I read that Question and answers too, but it did not help me. – Ahmet Batu Orhan Jun 14 '16 at 12:51
  • 1
    Please show me your js reference order in html as it working fine for me. – Shiva Jun 14 '16 at 12:54
  • 1
    You _have to_ have some HTML file that initiates this javascript. You can't load javascript directly into the browser and expect it to be executed. From your screenshot, I see you have `TestWebForm.aspx` - this is the script that generates your HTML. What is the content of it? – Aleks G Jun 14 '16 at 12:57

1 Answers1

2

You need to include Animal.js in the <head> section.

<head runat="server">
    <title></title>
    <script src="scripts/Animal.js"></script>
    <script src="scripts/World.js"></script>
    <script  type="text/javascript">
        world = new World();
        world.getOmnivores();
    </script>
</head>
J. Titus
  • 9,535
  • 1
  • 32
  • 45
  • Thanks a lot that works but I could not get it. Why we are not importing `Animal.js` to `World.js`. And it comes a little bit confusing cause I am using `Animal.js` file in `World.js` not int `TestWebForm.aspx`. Really I could not get it :( – Ahmet Batu Orhan Jun 14 '16 at 13:04
  • 1
    You're not using the `Animal.js` file in `World.js`. You're using the `Animal` object type in `World.js`. You needed to load that object type before using it. – J. Titus Jun 14 '16 at 13:08