locked
javascript call method in external .js file RRS feed

  • General discussion

  • hi i have two different .js files and im trying to call a method in the second .js file

    but i have no idea whats going on im sure that i have got the code right. but not working

     

    thanks for help :)

     

    // first_class.js

    var first_obj;

     

    var function first_class()

    {

       first_obj = new second_class("hello"); // this works

       first_obj.second_func(); // this doesnt

    }

     

    // second_class.js

     

    var hello;

     

    function second_class(hello)

    {

         this.hello = hello;

    }

    function second_func()

    {

       document.write(hello);

    }

    Friday, March 25, 2011 11:18 AM

All replies

  • second_func() above is simply a global level function, not associated with a class in any way.

    You need to use the prototype keyword when defining class functions.  Check out this and other articles on OO JS:

    http://www.codeproject.com/KB/aspnet/JsOOP1.aspx

    Alternatively, use prototype.js (or jquery, etc) helper function, such as Class.create():

    http://www.prototypejs.org/

    I could be more specific, but the articles on OO JS are very good, and will probably do a much more thorough job.

     


    Scottie05. ...30 years in software and trying not to count!
    Tuesday, August 2, 2011 6:00 PM