简体   繁体   English

在 IIFE 内部调用 function

[英]Call function inside IIFE

 let f = function(x) { alert(x) } (function() { f(1) }())

Why is this code throwing an error?为什么这段代码会抛出错误? At first, I thought the problem was connected with the incorrect syntax of the IIFE but then I learned that this syntax is also appropriate起初,我认为问题与 IIFE 的语法不正确有关,但后来我了解到这种语法也是合适的

This is the one of the rare cases where a semicolon is necessary to separate the function expression from the calling with the following parentheses.这是需要用分号将 function 表达式与使用以下括号的调用分开的罕见情况之一。

 let f = function(x) { alert(x) }; // <------------------- (function() { f(1) }())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM