立即调用函数表达式,而无需使用分组运算符 [英] Immediately invoked function expression without using grouping operator

查看:80
本文介绍了立即调用函数表达式,而无需使用分组运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to immediately invoke a function without using IIFE pattern (enclosing a function definition inside parentheses). Here I see two scenarios:

  1. 当立即调用功能声明时:给出SyntaxError .

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

立即调用函数表达式时:成功执行.

示例1:给出SyntaxError

//gives `SyntaxError`
function() {
    console.log('Inside the function');
}();

示例2:执行无误

// Executes without any error
var x = function() {console.log('Inside the function')}(); // Inside the function

所以,我有这些疑问:

  • 使用这种方法,为什么会给函数声明而不是函数表达式提供错误?
  • 有没有一种方法可以在不使用IIFE模式的情况下立即调用函数声明?
  • With this approach, why does it give an error for function declaration but not for function expression?
  • Is there a way we can immediately invoke a function declaration without using IIFE pattern?

推荐答案

在您的代码中,您没有该函数的名称,这是导致语法错误的原因.即使您有名字,也会抛出错误.

In your code you don't have name for the function that's the reason for syntax error. Even if you would had name it would have thrown error.

function func(){
  console.log('x')
}();

原因是函数声明没有返回函数的值,但是当您将函数声明包装在()内时,它强制它是返回值的函数表达式.

The reason is the function declaration doesn't return the values of the function however when you wrap function declaration inside () it forces it be a function expression which returns a value.

在第二个示例中,将function() {console.log('Inside the function')}视为表达式,因为它位于RightHandSide上.因此它执行时没有错误.

In the second example the function() {console.log('Inside the function')} is considered expression because it's on RightHandSide. So it executes without an error.

有没有一种方法可以立即调用函数声明而无需使用IIFE模式

Is there a way we can immediately invoke a function declaration without using IIFE pattern

您可以使用+将使函数声明成为表达式.

You can use + which will make function declaration an expression.

+function(){
  console.log('done')
}()

如果您不想使用+(),则可以使用

If you don't want to use + and () you can use new keyword

new function(){
  console.log('done')
}

@cat 在评论中提出了一个非常有趣的问题.我尝试回答.有三种情况

A very interesting question is asked by @cat in the comments. I try to answer it.There are three cases

+function(){} //returns NaN
(+function(){return 5})() //VM140:1 Uncaught TypeError: (+(intermediate value)) is not a function
+function(){return 5}() //5

+function(){}返回NaN

+在这里用作一元加号,它将其旁边的值解析为数字.由于Number(function(){})返回NaN,因此它也返回NaN

+function(){} returns NaN

+ acts as Unary Plus here which parses the value next to it to number. As Number(function(){}) returns NaN so it also returns NaN

通常使用()创建IIFE. ()用于制作函数声明,而+表达式是实现此功能的捷径.现在+function(){}已经是一个返回NaN的表达式.因此,调用NaN将返回错误.代码与

Usually IIFE are created using (). () are used to make a function declaration an expression + is short way for that. Now +function(){} is already an expression which returns NaN. So calling NaN will return error. The code is same as

Number(function(){})()

+function(){return 5;}()返回5

在上面的行中,+用于使语句成为表达式.在上面的示例中,第一个函数被调用,然后在其上使用+将其转换为数字.因此,以上行与

+function(){return 5;}() returns 5

In the above line + is used to make a statement an expression. In the above example first function is called then + is used on it to convert it to number. So the above line is same as

Number(function(){return 5}())

在语句证明中,"+在调用该函数后继续运行" 考虑以下代码段

In the proof of statement "+ runs on after the function is called" Consider the below snippet

console.log(typeof +function(){return '5'}());

因此在上面的代码段中,您可以看到返回的值是字符串'5',但是由于+

So in the above snippet you can see the returned value is string '5' but is converted to number because of +

这篇关于立即调用函数表达式,而无需使用分组运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆