回调函数小例

<html>
	<body>
			<script type="text/javascript">
					var person={};
					function setFun(name,callfun,sex,fun)
					{						
						person.name = name;
						person.sex = sex;
						callfun("软件工程师","初级工程师");	
						fun();		
					}
					
					function callback()
					{
						document.write("姓名:"+person.name+"<br/>");
						document.write("性别:"+person.sex+"<br/>");
						document.write("从事:"+arguments[0]+",级别:"+arguments[1]+"<br/>");
						document.write("参数长度:"+arguments.length)
					}
					
					setFun("jerry",function(td,type){callback(td,type)},"男",function(){alert("这是回调函数");});
			</script>
	</body>
</html>

回调函数是javascript里面一种很重要的机制,如果使用得当会让代码更简洁,效率更高。但是,回调函数不好之处就是对整个代码的逻辑变得更加复杂,不易于理解。

对于回调函数,我还是情有独钟的,虽然运用上面还不是很得当,但是如果能掌握其中的奥妙,会受益匪浅的

原文地址:https://www.cnblogs.com/xlhblogs/p/2398180.html