10

I'm just new comer at Javascript so when I read a Javascript document, and there're many complex structures that I cannot follow.

Here is the short explanation of Javascript code that I'm reading : in my case there are two main files : Helper.js and Circle.js.

In Helper.js, there is a method name :using:function(param1,param2). And below is code for Circle.js:

Helper.using('py.Figures', function (ns) {

    ns.Circle = function (params) {
        // some additional methods and code here
    }

    ns.Alert = function(){   // for the test purpose
           alert('hello');
    }
});

And in file test.html, I write some code like this :

<script src="Helper.js"></script>
<script src="circle.js"></script>
<script>
   test = function(){
        py.Figures.Alert();  // calling for testing purpose
   }
</script>
<body onload="test();"></body>

When I run on Chrome and view in console, I meet this error :

Uncaught TypeError: Object # has no method 'Alert'

It means I haven't import those class, yet. Please tell me how to calling function from another file. In my case is : calling Alert()

Thanks :)

@ Edit: I have added some links for the code :

Helper.js

Circle.js

hqt
  • 29,632
  • 51
  • 171
  • 250
  • I suggest uploading a fiddle so we will be able to see the whole operation. – Novak Jul 31 '12 at 18:51
  • Does `Helper.using` work? Does it do what it is supposed to do (creating the namespace)? – Felix Kling Jul 31 '12 at 18:52
  • *Helper.using('py.Figures'* doesn't look like JavaScript to me. – Diodeus - James MacFarlane Jul 31 '12 at 18:53
  • `test` is being invoked before `py.Figures` is loaded. Generally, when doing a dynamic load like this, one does stuff in the *callback* of the loader. –  Jul 31 '12 at 18:53
  • @FelixKling I have edited my post for the code of `Helper.js`. I'm not sure, but I think ns is stand for namespace – hqt Jul 31 '12 at 18:58
  • @pst: `Helper.using` does not seem to be asynchronous, so it should work. Besides, `py.Figures` seems to exist, otherwise it would throw an other error. @Diodeus: Why not? – Felix Kling Jul 31 '12 at 19:02
  • @FelixKling yes, `py.Figures.Alert()` is how the writer this code tell me, but when I run, I met that error :( – hqt Aug 01 '12 at 03:28
  • possible duplicate of [Include a JavaScript file in another JavaScript file?](http://stackoverflow.com/questions/950087/include-a-javascript-file-in-another-javascript-file) – koullislp Jun 03 '15 at 10:41

2 Answers2

11

Why don't you take a look to this answer

Including javascript files inside javascript files

In short you can load the script file with AJAX or put a script tag on the HTML to include it( before the script that uses the functions of the other script). The link I posted is a great answer and has multiple examples and explanations of both methods.

Community
  • 1
  • 1
Samuel Lopez
  • 2,360
  • 6
  • 29
  • 39
1

Yes you can. Just check my fiddle for clarification. For demo purpose i kept the code in fiddle at same location. You can extract that code as shown in two different Javascript files and load them in html file.

https://jsfiddle.net/mvora/mrLmkxmo/

 /******** PUT THIS CODE IN ONE JS FILE *******/

    var secondFileFuntion = function(){
        this.name = 'XYZ';
    }

    secondFileFuntion.prototype.getSurname = function(){
     return 'ABC';
    }


    var secondFileObject = new secondFileFuntion();

    /******** Till Here *******/

    /******** PUT THIS CODE IN SECOND JS FILE *******/

    function firstFileFunction(){
      var name = secondFileObject.name;
      var surname = secondFileObject.getSurname()
      alert(name);
      alert(surname );
    }

    firstFileFunction();

If you make an object using the constructor function and trying access the property or method from it in second file, it will give you the access of properties which are present in another file.

Just take care of sequence of including these files in index.html