Lawrence DeSouza
Lawrence DeSouza

Reputation: 1027

Re-coding an undefined javascript function

I "borrowed" this snippet from another site, and I've realized that it does not work when I try to use it. What is wrong with it?

var LessonsCustomControl = (function LessonsCustomControl_constructor(){

    var LessonTypes=[], LessonInterfaces={}, LessonsContent={};
    
    LessonInterfaces.ListQuiz1 = (function(typeClass){
        ...
    }

    function initListQuiz1(){
        LessonInterfaces.ListQuiz1('FITBPickOption').init();
    }

})();

In my code, when I try to call initListQuiz1(), I am getting nothing, i.e. LessonsCustomControl is undefined. Need some help in rewriting this in valid javascript or jquery.

Upvotes: 0

Views: 44

Answers (1)

Guerric P
Guerric P

Reputation: 31815

The code you borrowed features encapsulation with an IIFE.

In order to use some functions from outside it, you have to return them:

var LessonsCustomControl = (function LessonsCustomControl_constructor(){

    var LessonTypes=[], LessonInterfaces={}, LessonsContent={};
    
    LessonInterfaces.ListQuiz1 = (function(typeClass){
        ...
    }

    function initListQuiz1(){
        LessonInterfaces.ListQuiz1('FITBPickOption').init();
    }

    return {
        initListQuiz1,
        // Other functions you need to "export"
    }

})();

Then call:

LessonsCustomControl.initListQuiz1();

Upvotes: 1

Related Questions