How to access function from outside an extended function?

I have this code

(function ($) {
    $.fn.Val_Rev2 = function (containerId) {
        let container = document.getElementById(containerId);
        let allFields = container.querySelectorAll("[is-required]");
        let errorList = [];

        // A bunch of other code....
        
        function PrintErrorSummary() {
            // Do stuff
        }
    }
}(jQuery));

and I can access the Val_Rev2 by doing

$(this).Val_Rev2("container");

I don’t know how to access PrintErrorSummary() and I need to on a button click event so I can see errorList that gets populated from other code in the Val_Rev2

I could just pass in another id for a button and bind an event to that so I can access the errorList, but would like to know of any other ways to access the PrintErrorSummary

I tried…

Answer

I’m not sure what Val_Rev2 function would return, but I think PrintErrorSummary function needs the defined variables in Val_Rev2 to print something.

You can do something like this:

(function ($) {
$.fn.Val_Rev2 = function (containerId) {
    let container = document.getElementById(containerId);
    let allFields = container.querySelectorAll("[is-required]");
    let errorList = [];

    // A bunch of other code....
    
    function PrintErrorSummary() {
        // Do stuff
    }

    return {
         PrintErrorSummary: PrintErrorSummary,
         // other stuff if needed
    }
}
}(jQuery));

And you can access the function as shown below:

$(this).Val_Rev2("container").printErrorSummary();