回调函数


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>Document</title>
</head>
<body>
	<script>
	function weixin(){
		console.log("boss,I got it");
	}

	function callFunction(callback){//此处callback需要和3处的函数保持一致;
		console.log("ok");
		callback();//这是3;
	}

	callFunction(weixin);//函数名可以作为参数被传递;

		
	</script>
</body>
</html>
执行结果:


三种错误:

1.callFunction里面不填写参数;
callFunction()//callFunction 里面不填写参数。
执行结果:
 
2.callFunction里面填写任意参数;
callFunction(callback1);
 3.callFunction()里面填写基本数据类型;

callFunction(1);
                                                               总结:                                                                   
        1.当参数为空或者为基本数据类型时,callFunction函数会执行console.log("ok");然后在执行到callback()(注意此时callback不是函数,无法调用)时报错;
        2.当参数为未定义时,此时直接报错;

原因:  
                                                参见函数执行的步骤

 
                    so,在使用回调函数时,要保证执行函数的参数与回调含函数名一致。            






posted @ 2017-09-24 19:38  夏小北666  阅读(119)  评论(0编辑  收藏  举报