Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the formats of immediate execution functions in JavaScript

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces JavaScript in the immediate execution of the function format, the article is very detailed, has a certain reference value, interested friends must read!

Sometimes I see some amazing functions in JavaScript, such as the screenshot below:

This function will run automatically as long as the browser loads, and it needs to be called. This function was also mentioned earlier in the closure, which is generally called: immediate execution function.

Characteristics of immediate functions:

automatically performs

It's only gonna happen once.

Execute function format immediately

The immediate execution function generally has the following format:

#Format 1 (//W3C recommends this writing)(function (){ }()) #Format 2 (but this is one of the most common ways)(function (){ })();

In fact, the above can also be seen immediately execute the function, generally do not write the function name, its meaning is not big, after all, the immediate function does not have to be called by the function name, this and literal definition of the function when the effect is similar.

Let's take an example of how closure and immediate execution combine:

var fun=(function(){ function test(a,b){ console.log(a+b); } return test;})()

Execute Function Immediately Other Ways-Expression

The format definition of the immediate execution function above, but there are other ways to implement this function, such as the following is not the format above.

! function(){ console.log("test");}()

看出其输出的两行,第一个是test,也就是有种立即执行函数的结果,而true被输出是是因为前面多了!,这个在隐式转换的时候说过,在!后面的都会强制换行布尔类型。

现在又有一个疑问了,为什么这种方式可以实现呢?

分析这种情况的产生,首先来一个表达式函数:

var test=function(){ console.log("test表达式")}#这样声明后,如何使用呢?如下test();

这个时候就有一个大胆的想法了,赋值的函数,通过变量值+()就可以执行,那为什么不能直接写好。

var test=function(){ console.log("test表达式")}()

这地方可以看出 表达式函数也有了一种立即执行的效果。

(补充:为什么都有undefined,因为这个函数本身就没有return 值,所以会有undefined这个值在浏览器console面板输出了undefined)

那直接在函数后面放括号可以吗?

function(){ console.log("test表达式")}()

可以看出需要用表达式前提才可以在后面放(),不然会报错。

这个时候就有了一个大胆的想法,其实!后面方法,其实将函数转换成立表达式函数,所以后面可以+()可以直接运行了。那就有了大胆的想法,既然如此那直接在函数前加一个加号(+)试试。

+function(){ console.log("test表达式")}()

既然加号可以那么:

+-!~

当然所谓的乘号和除号是无法实现的,还有一个神奇的关键字也可以有这个神奇的效果,那就是new 和 void

new function(){ console.log("test表达式")}()

还有一个那就是量逻辑运算符号 || 和&& 也可以实现

不过这个需要根据其特征再前面需要加真或者假才可以,单独使用是不可以的

1 && function(){ console.log("test表达式")}() 或者0 || function(){ console.log("test表达式")}()

还有一个神奇的符号(逗号)也可以实现这个功能:

1,function(){ console.log("test表达式")}()

可以看出如果使用逗号,无论前面真假都会执行后面的表达式函数。

立即执行函数可以带参数

立即执行函数也可以有参数的,具体如下

(function(a,b){ console.log(a,b)})(1,2)

应用

这个题是很经典的面试题,首先创建下一个html

test 这是第一条 这是第二条 这是第三条 var liList=document.getElementsByTagName('li'); for(var i=0;i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report