0

I am implementing the Revealing Module pattern in JavaScript and having difficulty in accessing the declared variable in another script. Below is my code.

Script1:

var SomeEventHandler = (function (){

    var logSomeEvent = function(){...}
    return {
        trackEvent: logSomeEvent;
    };
})();

Script2:

SomeEventHandler.trackEvent(); // This gives me undefined error. 

In the HTML, I have added script 1 before script 2, so I wanted to know how can i access SomeEventHandler in script 2.

Mandar
  • 498
  • 1
  • 4
  • 17
  • 1
    Please include your html. My guess is that you don't have the scripts declared in the correct order. – Andrew Eisenberg Jul 31 '14 at 18:47
  • I am working in Visualforce so not sure if including that page makes sense. But I have made sure that script order is right – Mandar Jul 31 '14 at 18:49

1 Answers1

4

I noticed that you have a semicolon in your object notation. Multiple key:value properties in objects created with object-notation are separated by commas, not semicolons. Also, you don't need the separator if there is only one element. I removed the semicolon and it works fine in my testing.

var SomeEventHandler = (function (){
    var logSomeEvent = function() { console.log('Cool stuff happened!'); }
    return {
        trackEvent: logSomeEvent
    };
}());

// ...

SomeEventHandler.trackEvent(); // Cool stuff happened!
cvializ
  • 616
  • 3
  • 11