Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
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 Answer 1

up vote 4 down vote accepted

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!
share|improve this answer
    
Thank you very much! this fixed the issue :) –  Mandar Jul 31 '14 at 19:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.