Calling function defined in an IIFE function from HTML

后端 未结 2 705
日久生厌
日久生厌 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:48

    Since you declare print_name with var, its scope is local to the IIFE, so you can't call it from outside there. You need to assign to window.print_name to make it global.

    (function mainIIFE() {
        "use strict";
        window.print_name = function(first, last) {
            console.log(first + " " + last);
        };
    }());
    

    It's also not clear why you're using new to call the function, since it doesn't fill in an object.

提交回复
热议问题