1

Hello fellow programmers,

I am wondering today how to access objects from another external js file. I'm using this as a way to organize my code throughout multiple js files. Here's an example of what I'm trying to say.

Imagine this as the code from an external js file:

$(function () {

  function Person() {
    this.name = "Bob";
  }

})

And I want to access that object in another js file:

$(function () {

  var person = new Person;
  alert(person.name);

})

Is there a way to do something like that? How would I need to position the html?

4
  • If Person is defined like you poster then - it's impossible to access it in another file. Commented Sep 5, 2016 at 16:32
  • @dfsq what do you mean "like you poster then" Commented Sep 5, 2016 at 16:34
  • The problem is answered, it took forever to figure out, all I had to do was put the javascript files all AFTER the canvas elements Commented Sep 5, 2016 at 17:15
  • I meant "like you posted". Commented Sep 5, 2016 at 18:02

3 Answers 3

1

My first wonder is why you have your JS wrapped in a function like so. You can see here I've accessed "hello" from another script loaded after it's set - as it's set in the global space.

https://jsfiddle.net/sj7bp97c/

<script src="https://pastebin.mozilla.org/?dl=8907696">
</script>
<script src="https://pastebin.mozilla.org/?dl=8907697">
</script>

One script sets the value, the other prints it to console. Unless it's required for your Javascript to be surrounded by functions, I'm not sure why you're doing so.

Sign up to request clarification or add additional context in comments.

8 Comments

It's because people have been saying that I have to include it for jQuery or something like that.
Unfortunately, I want my code sent out in a file not a url
@Bob All the Javascript is found at those two links (representing external files). pastebin.mozilla.org/8907696 is the first one, and pastebin.mozilla.org/8907697 is the second one.
Here is an example of jQuery without the surrounding functions. jsfiddle.net/sj7bp97c/1
@Bob there's no difference between a file and a URL. The code is loaded the same, the methodology is the exact same. If you copied the bodies from both those files, made them local scripts, and included them in your HTML, it would work the same.
|
0

Reveal the function to the global scope:

$(function () {

  function Person() {
    this.name = "Bob";
  }

  return {
    Person: Person 
  }

})()

When revealed to the global scope, you access it from within another JavaScript file as soon as the script has initiated. So you'll want to include it after you included this one.

Comments

0

You just need to mention both of your js script files in head section of html.

Javascript will take care rest of the things. <head> <script src="First.js"></script> <script src="Second.js"></script> </head>

5 Comments

It said that Person was undefined
Just write below mentioned code in second js file. function Person() { this.name = "Bob"; }
You have also made a mistake while declaring new object, it should be var person=new Person()
That in fact does not matter, it will work either way for me
Your answer did not help me at all and actually delayed me in solving me issue because my script tags weren't in the head section at all but at the very end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.