Results 1 to 7 of 7
  1.    #1  
    I have three pages that each call the same function. Right now the code is repeated in the three assistant.jsjsjs $files$. $While$ $that$'$s$ $functional$, $it$'$s$ $clearly$ $not$ $the$ $best$ $option$.

    When code says, "this.handleButton" I think "this" means that the handleButton function is somewhere on this page. And if it does, how do I then get it to look for the function on another page?

    Second, is is better to put that common function in the stage-assistant.jsjsjs $file$ $or$ $create$ $a$ $new$ $file$, $putting$ $it$ $in$ $its$ $own$ $javascripts$/ $folder$?

    Thanx in advance.
  2. #2  
    this always means the object the code being called lives in. So let's say we have an object:

    foo = {
    member1: 1,
    member2: 'whatever',
    myMethod: function() {
    // do something
    },
    anotherMethod: function() {
    // let's call the myMethod function
    this.myMethod();
    }
    }

    So, if we call foo.anotherMethod() it will use myMethod which is called as this.myMethod() in that particular context.

    Hope this makes enough sense?
    My shiny new TouchPad apps: Scientific RPN Calculator HD - Screamager HD
  3. #3  
    What I do for this is create a folder called lib under the app folder. The put in common funtions in a file there. Put it is the souces list. Now these functions are globally callable.
  4.    #4  
    Thank you. Both of those answers do help, but I need a tiny bit more.

    How do I call a global function? What is different about calling a function within an object vs. calling one outside of the object?

    Once I see the answer I will probably feel foolish for asking it, but right now I can't see it.

    I am trying to educate myself. I'm reading over w3schools.com Javascript refresher and I'm on the Prototype website, trying to figure that out, too.
  5. rmbwebs's Avatar
    Posts
    129 Posts
    Global Posts
    145 Global Posts
    #5  
    Quote Originally Posted by Colonel Kernel View Post
    Thank you. Both of those answers do help, but I need a tiny bit more.

    How do I call a global function? What is different about calling a function within an object vs. calling one outside of the object?

    Once I see the answer I will probably feel foolish for asking it, but right now I can't see it.

    I am trying to educate myself. I'm reading over w3schools.com Javascript refresher and I'm on the Prototype website, trying to figure that out, too.
    You just call it from anywhere. It's globally accesible.

    Like alert() is a globally accessible function. Mojo is a globally accessible object.
  6. #6  
    Just one friendly tip: It's considered bad practice to create global functions and objects all over the place when not absolutely necessary (almost never). The bigger your app gets, the harder it will get to keep track of all that stuff.
    My shiny new TouchPad apps: Scientific RPN Calculator HD - Screamager HD
  7. #7  
    The best thing to do is to create one or more classes that contains the functions you may use in different scenes or even in other classes.

    You can use the Prototype Class.create syntax. You can then create an instance of the class in each scene where it is needed or you can create a global instance where warranted. Classes are very powerful and can contain properties in addition to functions.

    I would recommend that you read the following docs. The Prototype framework is integrated into Mojo so you can the Prototype class functionality in your apps.

    Prototype JavaScript framework: Class
    Prototype JavaScript framework: Class.create

    You may also want to read some basic articles about the concepts of OOP(Object Oriented Programming), especially with regard to its use in Javascript. This will help you understand how to use classes and their instances.

Posting Permissions