0

I've been looking for answers why my javascript code is totally on my webhost. I have existing javascript functions that used to work fine until I tried to modify them using the revealing module pattern in javascript because from what I gathered this is good practice to reduce polluting the global namespace. There are no function calls outside this scope so I did not use return to point to functions. It worked okay in different browsers while testing from Visual Studio.

Is there something maybe in godaddy server that I need to configure? I tried to call them for support but no help. Below is sample of my code. I actually have more functions in this scope but kept it short for this question. Thanks in advance.

myModule = (function () {

    //add upDate
    function timeOfUpdate {
    var timeOfUpdate = new Date(document.lastModified);
        $("#lastUpdate").append(timeOfUpdate);

    }
    //add some more functions
    function modTwo {
    //add code to execute..
    }
})();
Jørgen R
  • 10,568
  • 7
  • 42
  • 59
smundoKo2
  • 3
  • 1

1 Answers1

2

I'm not sure exactly what you're trying to do with that module definition, but you aren't returning anything from the module so the functions inside the module are local within the module and can't be used from outside the module. If you look at this reference on the revealing module pattern, you will clearly see that they are returning an object with methods on it and that is what is assigned to the module variable.

Perhaps what you meant to do was this:

var myModule = (function () {

    return {
        //add upDate
        timeOfUpdate: function {
            var timeOfUpdate = new Date(document.lastModified);
            $("#lastUpdate").append(timeOfUpdate);

        },
        //add some more functions
        modTwo: function {
            //add code to execute..
        }
    }
})();

This returns an object from the IIFE so that myModule becomes an object that has your methods timeOfUpdate() and modTwo() as properties on the object.

Then, you could access your functions as:

myModule.timeOfUpdate();
myModule.modTow();

If you're not trying to use any of these functions from outside the module, then you'll have to help us with more details about what you're trying to do and what errors you see. You can wrap a bunch of code in an IIFE like you were doing just fine and that will cause no problems. There is ZERO point in assigning to a module name as in myModule = ... if you aren't returning anything from that IIFE so it appears to me like you're at least going in a wrong direction there, but without understanding what you're trying to do with this module or what errors you are running into, we can't really help further.


Also, your example shows some local function definitions, but doesn't show calling any functions so nothing would happen until you insert some code that actually calls your functions.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Yes jfriend00. Thank you. My script is working now. Calling the main object and attaching the timeOfUpdate() was the part I am missing. I was locked into the thought that because I had the self-invoking operator at the end of myModule value would auto invoke the methods I had inside its scope without making a call. I appreciate your pointing me to that problem. – smundoKo2 Feb 11 '15 at 16:22
  • @smundoKo2 - since it appears you may be new to StackOverflow, are you aware that when you get your question answered, you can indicate the "best answer" to your question by clicking the green checkmark to the left of the answer. That will earn both you and the person who provided the answer some reputation points here at StackOverflow. An accumulation of reputation points will earn you more privileges here. – jfriend00 Feb 11 '15 at 16:26