0

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.

3
  • Does this answer your question? Why does this JavaScript code print "undefined" on the console? Commented Jul 11, 2021 at 20:39
  • LessonsCustomControl_constructor doesn't return anything, therefore you can't access anything that is generated inside that function, including ListQuiz1 Commented Jul 11, 2021 at 20:40
  • Thanks, I didn't even realize that for a whole week, but it is obvious. I have re-written everything without the need for returning - this aspect was not suited for my purpose. Commented Jul 11, 2021 at 21:21

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.