Sign in to follow this  
Concentrate

javascript: loading in external js file problem

Recommended Posts

Concentrate    181
In my index.html, I use the regular script tag to load in .js files.

The problem I guess is that one js file is not seeing a the constructor for another object which is located is another js file. For example :

//foo.js
f = function(){ var o = new Bar(...); }
//Bar.js
var Bar = { name: "Bar" };
//index.html
<script /* load in bar.js */ >
<script /* load in foo.js */ >


The error says that Bar is not a constructor?

I am unsure on how to solve this problem, because I haven't used js much. Any advice? Also foo and bar is just an example but the actual
files are much more longer and there are more than 2

Share this post


Link to post
Share on other sites
CadetUmfer    234
Bar isn't a constructor, it's an object. If you want Bar to be a constructor, it should be:

[code]function Bar() {
this.name = "Bar";
}[/code]

Share this post


Link to post
Share on other sites
Concentrate    181
Sorry that was a typo, in the actual code it is defined as the constructor. Anyways I fixed the problem. The problem was that the files weren't getting loaded completely. So I had to put the link tag at the bottom of the file for it to do proper linking and loading.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this