Calling function defined in an IIFE function from HTML

后端 未结 2 733
日久生厌
日久生厌 2021-01-29 11:26

I have a IIFE function in a file called test.js i.e.

(function mainIIFE() {
    \"use strict\";
    var print_name = function(first, last) {
        console.log(         


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-29 11:45

    You can return the function and store it in a variable. This is essentially the same as assigning it to window.print_name inside the IIFE since the new variable will be in global namespce

    var print_name = (function mainIIFE() {
        "use strict";
        var print_name = function(first, last) {
            console.log(first + " " + last);
        };
        return print_name;
    }());
    
    print_name('Foo','Bar')

提交回复
热议问题