• 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>
    

      

  • 相关阅读:
    在Linux服务器上添加ip白名单允许ssh登录访问
    crontab + shell脚本实现文件重命名
    mysql数据库提示ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
    附加题2:中文编程的发展角度
    附加题1:实体店的未来存在形式
    第八周作业
    第七周作业
    第六周作业
    第五周作业
    第四周作业
  • 原文地址:https://www.cnblogs.com/yiweiyihang/p/7661527.html
Copyright © 2020-2023  润新知