我正在尝试不使用IIFE模式(将函数定义放在括号内)立即调用函数。在这里,我看到两种情况:
When a function declaration is invoked immediately: gives SyntaxError.
When a function expression is invoked immediately: executes successfully.
Example 1: gives SyntaxError
//gives `SyntaxError`
function() {
console.log('Inside the function');
}();Run Code Online (Sandbox Code Playgroud)
Example 2: Executes without any error
// Executes without any error
var x = function() {console.log('Inside the function')}(); // Inside the functionRun Code Online (Sandbox Code Playgroud)
So, I have these doubts: