简体   繁体   English

JavaScript IIFE 评估未定义

[英]JavaScript IIFE Evaluation is undefined

Hello I'ma beginner at JavaScript and I'm trying to figure out how IIFE work, and I still have some trouble.您好,我是 JavaScript 的初学者,我正在尝试弄清楚 IIFE 是如何工作的,但我仍然遇到了一些麻烦。 In the lecture I stumbled upon this example code, and the evaluation of the code below gives the result „undefined“ whereas calling the function with „add()“ returns 1 and I really don't understand why, since function is an IIFE.在讲座中,我偶然发现了这个示例代码,对下面代码的评估给出了“未定义”的结果,而用“add()”调用 function 返回 1,我真的不明白为什么,因为 function 是一个 IIFE。

var add = (function() {
       var counter = 0;

       return function() {
            counter += 1;
            return counter 
       }
})();
//Output: undefined

You IIFE is a factory function . You IIFE 是工厂 function In this case it returns a function, so that works.在这种情况下,它返回一个 function,这样就可以了。 You can assign the result to a variable and subsequently run the resulting function.您可以将结果分配给一个变量,然后运行生成的 function。 The counter variable is ' closed over ', it will be manipulated (and returned) via the returned function, but it will not be accessible to the 'outside world'. counter变量是“ 关闭的”,它将通过返回的 function 进行操作(并返回),但“外部世界”将无法访问它。

 // assign the result to a variable const add = (function() { let counter = 0; return function() { counter += 1; return counter; } })(); // add is a function: execute it a few times add(); add(); console.log(add()); // => 3

There's nothing wrong with your IIFE.您的 IIFE 没有任何问题。 You are only seeing the result of the var statement.您只看到var语句的结果。 Examples:例子:

var x = function() {};
//=> undefined
var x = 42;
//=> undefined
var x = true;
//=> undefined

An assignment expression will evaluate to the value on the right:赋值表达式将计算右侧的值:

y = 10;
//=> 10

But the var statement itself returns nothing:但是var语句本身什么也不返回:

var y = 10;
//=> undefined

Relevant part of the spec:规范的相关部分:

VariableDeclaration: BindingPattern Initializer变量声明:BindingPattern 初始化器

  1. Let rhs be the result of evaluating Initializer.令 rhs 为计算 Initializer 的结果。
  2. Let rval be?让 rval 成为? GetValue(rhs).获取值(右)。
  3. Return the result of performing BindingInitialization for BindingPattern passing rval and undefined as arguments.返回执行 BindingPattern 的 BindingInitialization 的结果,传递 rval 和未定义为 arguments。

See https://tc39.es/ecma262/#sec-variable-statementhttps://tc39.es/ecma262/#sec-variable-statement

For IIFE you need to wrap your anonymous function in "( )" and then invoke it with "();"对于 IIFE,您需要将匿名 function 包装在 "( )" 中,然后使用 "();" 调用它It does not allow declaration.它不允许声明。 try removing declaration.尝试删除声明。

(function() {
   var counter = 0;

   return function() {
        counter += 1;
        return counter 
   }
})();

For more info refer this.有关更多信息,请参阅此。 https://www.tutorialsteacher.com/javascript/immediately-invoked-function-expression-iife https://www.tutorialsteacher.com/javascript/immediately-invoked-function-expression-iife

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

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