Uncaught TypeError: (intermediate value)(...) is not a function

(function ($) {
            console.log($('div').html())
        })(jQuery)

        
        (function ($) {
            console.log($('h1').html())
        })(jQuery);

ECMAScript规范具有分号自动插入规则,但是在上面代码中,在第一个立即执行函数末尾却不会插入,因为第二个立即执行函数,会被解释为如下形式:

(function ($) {
            console.log($('div').html())
        })(jQuery)(function ($) {
            console.log($('h1').html())
        })(jQuery);

因此,我们必须在第一个立即执行函数的末尾添加分号:

(function ($) {
            console.log($('div').html())
        })(jQuery);
        
        (function ($) {
            console.log($('h1').html())
        })(jQuery);

版权声明:本文为qq_40713392原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。