js立即执行函数

本文深入探讨了JavaScript中立即执行函数的概念与应用,通过多个示例展示了如何使用立即执行函数进行参数传递、返回值处理及执行特殊操作。文章还介绍了立即执行函数在保持代码简洁性和避免全局作用域污染方面的优势。

js立即执行函数

直接po图和代码

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js立即执行函数</title>
<script type="text/javascript">
//立即执行函数
( function (){console.log("666")} )();
( function (){console.log("888")}() );
( function a(){console.log("江西省赣州市于都县")} )();
( function b(){console.log("江西省于都县")}() );

//带参数
( function (who){console.log("I Love You " + who)} )("韦小宝");
	var hometown = "江西省赣州市于都县渡江大道66666号";
( function (address){console.log("我老家在" + address)} )(hometown);
( function (parameter){console.log("-----" + parameter + "-----")} )(this);

console.log("this = " + this);

//返回值
var result = ( function (){return 999} )();
console.log("result = " + result);

//函数表达式加上执行符号,就形成了立即执行函数
+function test() {console.log("AAA")} ();
-function test() {console.log("BBB")} ();
!function test() {console.log("CCC")} ();
</script>
</head>
<body>
<h1>js立即执行函数</h1>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值