js回调,apply,call 函数学习

来源:互联网 发布:java https cookie 编辑:程序博客网 时间:2024/05/16 10:50
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'callBack.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><script type="text/javascript">//测试身高是否达到标准function testHeight(height,callback){   if(height<170){        alert("卧槽三级残废");   }else if(height>=170 && height<=180){alert("男神身高");var me=this || window;var name="dandan";var age="23";//方法一//callback("你是xiaoer吧","22");//方法二 //callback.call(me,name,age);//方法三//arguments[1].call(me,name,age);//方法四  调用callback的apply方法 ,但是this指向{name:'taolong'}的(第一个参数的内容),apply传递参数是以数组的形式传递callback.apply({name:'taolong'},[name,age]);   }else{alert("太高啦吧");   }};function startTest(){   var num=document.getElementById("score").value;testHeight(num,function(msg,age){alert("恭喜你一定是大受欢迎的男人:"+msg+"  "+age+"  "+this.name);});}// (function(){// testHeight(180,function(){// alert("恭喜你一定是大受欢迎的男人");// });// })();</script>  </head>    <body>请输入你的身高<input  type="text"  id="score"> <input type="button" onClick="startTest()" value=" 看看结果">  </body></html>

0 0