相关疑难解决方法(0)

立即调用函数表达式,无需使用分组运算符

我正在尝试不使用IIFE模式(将函数定义放在括号内)立即调用函数。在这里,我看到两种情况:

  1. When a function declaration is invoked immediately: gives SyntaxError.

  2. 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 function
Run Code Online (Sandbox Code Playgroud)

So, I have these doubts:

  • With this approach, why does it give an error for function declaration but not for function expression?
  • Is there …

javascript function iife

24
推荐指数
2
解决办法
1393
查看次数

标签 统计

function ×1

iife ×1

javascript ×1