javascript中的回调函数

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>回调函数使用</title>
	</head>

	<body>
		<!--一。基本用法-->
		<script language="javascript" type="text/javascript">
			//回调函数基本用法:
			//		function dosomeThing(callback){
			//			callback("我是","回调",'函数');
			//		};
			//		function foo(a,b,c){
			//			alert(a+b+c);
			//		};
			//		dosomeThing(foo);

			//匿名函数中使用回调函数:
			//			function dosomeThing(domsg, callback) {
			//				alert(domsg);
			//				if(typeof callback == "function") {
			//					callback("回调函数中的","参数");
			//				};
			//			};
			//			dosomeThing("匿名函数", function(a,b) {
			//				alert("此处是匿名函数中使用回调函数;"+a+b);
			//				
			//			});
		</script>

		<!--二。高级用法-->
		<!--<script language="javascript" type="text/javascript">
			function Thing(name) {
				this.name = name;
			};
						Thing.prototype.doSomething = function(callback) {
							callback.call(this);
						};
			function foo() {
				console.log(this.name);
			};
			var t = new Thing("Joe");
			t.doSomething(foo);
		</script>-->
		<!--传参数-->
		<!--<script type="text/javascript">
			function Thing(name){
				this.name=name;
			};
			Thing.prototype.doSomething=function(callback,parameterss){
				callback.call(this,parameterss);
			};
			function foo(parameterss){
				console.log(parameterss+" "+ this.name);
			};
			var t=new Thing("Joe");
			t.doSomething(foo,"Hi");
		</script>-->
		<!--使用 javascript 的 apply 传参数-->
		<script type="text/javascript">
			function Thing(name) {
				this.name = name;
			};
			Thing.prototype.doSomething = function(callback) {
				callback.apply(this, ['Hi', 3, 2, 1]);
			};

			function foo(parameterss, three, two, one) {
				console.log(parameterss + " " + this.name + " " + three + two + " " + one); //输出:Hi Joe321
			};
			var t = new Thing("Joe");
			t.doSomething(foo);
		</script>
	</body>

</html>

  

原文地址:https://www.cnblogs.com/yiweiyihang/p/7661527.html