JS匿名函数核心 16

100 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>JS匿名函数核心 16</title>
</head>
<body>
	<script>
		/*
        1.什么是匿名函数?
        匿名函数就是没有名称的函数
		*/
	//第一作为其他函数的参数
	/*function say(fn)//fn=
	{
		fn();
		//这一步相当于
		function()
		{
			console.log("hello  world");
		}();
		//注意点就是必须在匿名函数的前后加上小括号。这是写法.
	}
	say(function()
	{
		console.log("hello  world");
	});*/
	//解析:.....
	/*(function()
	{
		console.log("hello  world");
	})();*/





	//第二作为其他函数的返回值
	/*
        function test() {
            return function () {
                console.log("hello lnj");
            };
        }
        let fn = test(); // let fn = say;
        fn();
        */
       //下面为解析,加个变量即可:
		function test() {
            let say= function () {
                console.log("hello lnj");
            };
            return say;
        }
        let fn = test(); // let fn = say;

        fn();//这一步相当于
       	 (function () {
                console.log("hello lnj");
            })();
            //完成
        
	</script>
</body>
</html>