zoukankan      html  css  js  c++  java
  • js匿名函数测试

    js匿名函数测试

    	<!DOCTYPE html>
    	<html>
    
    	<head>
    	<meta charset="UTF-8">
    	<title>js匿名函数测试</title>
    	</head>
    
    	<body>
    
    	<input type="button" value="点击" id="btn">
    	<script type="text/javascript">
    		//匿名函数的第一种情形
    		//绑定事件
    		var btn = document.querySelector("#btn");
    		btn.onclick = function() {
    			alert("aaaaa");
    		}
    		//匿名函数的第二种情形
    		setInterval(function() {
    			//alert("bbbbb");
    		}, 5);
    		//匿名函数的第三种情形
    		var fun = function() {
    			alert("ccccc");
    		}
    		fun();
    		//匿名函数的第四种情形
    		var obj = {
    			name: "dddd",
    			say: function() {
    				alert(this.name);
    			}
    		}
    		obj.say();
    
    		(function() {
    			console.log(123);
    			alert(123);
    		})();
    
    		(function(a) {
    			console.log(a);
    			alert(a);
    		})("hello world!");
    
    		/*
    		 
    		 * 匿名函数
    		 * function(){
    		 * 	console.log(A);
    		 * }
    		 * 
    		 * ()()直接调用
    		 * (function(){
    		 * 	console.log(123);
    		 * })()
    		 * 
    		 * 
    		 * (function(a){
    		 * 	console.log(a);
    		 * })("hello world!")
    		 * 
    		 * 匿名自执行函数的作用
    		 * 1.匿名自执行函数最常见的作用是用于实现闭包的情况中。
    		 * 闭包:闭包是js的一种特性,我们可以通过闭包实现函数内外部的连接,
    		 * 并且可以使得函数的局部变量始终存在于内存中。
    		 * 2.匿名自执行函数还可以用于在js中模拟创建块级作用域,
    		 * 即如果使用匿名自执行函数将某些代码包裹起来可以实现块级作用域的效果,
    		 * 减少全局变量的数量,在匿名自执行函数执行结束后变量就会被内存释放掉,从而也会节省了内存。
    		 * 
    		 * 匿名函数可以简单理解为没有名字的函数,常见的场景一共就有4种。
    		 * 匿名自执行函数可以简单理解为可以自己执行的匿名函数,实现匿名自执行函数的方式一共有4种。
    		 * 匿名自执行函数的作用就是用于闭包和创建独立的命名空间两个方面。
    		 * 
    		 * 
    		 * 
    		 * */
    			</script>
    		</body>
    	</html>
    
  • 相关阅读:
    78. Subsets
    93. Restore IP Addresses
    71. Simplify Path
    82. Remove Duplicates from Sorted List II
    95. Unique Binary Search Trees II
    96. Unique Binary Search Trees
    312. Burst Balloons
    程序员社交平台
    APP Store开发指南
    iOS框架搭建(MVC,自定义TabBar)--微博搭建为例
  • 原文地址:https://www.cnblogs.com/renxiuxing/p/9700566.html
Copyright © 2011-2022 走看看