所有 ctype 执行 pow(x 意义 title tlist pre
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>
- </title>
- </head>
- <body>
- <p id=' box'>
- </p>
- <script>
- //1.定义函数有几种方式
- /*
- 1、调用关键字来构造
- function sum(x,y){
- return x+y;
- }
- 2、使用Funtion()构造函数
- var f = new Function( " x " , " return x*x; " );
- console.log(f(2));
- 3、函数直接量声明函数
- var f = function (x) {reurn x * x};
- */
- //2.retrun关键字的意义
- /*
- 1、返回控制与函数结果
- return 表达式;
- 在函数语句借宿时执行,并返回表达式的值作为函数的结果
- 2、返回控制
- 返回空值,语法return: false;
- 阻止默认事件
- 3、中断函数执行
- */
- //3.函数是否可以重定义,会有什么事情发生
- /*
- 不能重定义,后面会覆盖前面
- */
- //4.var和function关键字有什么特别功能?
- /*
- 变量提前声明
- */
- //5.如何理解变量的作用域?
- /*
- 能够使用函数的位置,分为局部作用域,全局作用域,
- (1)最外层函数和在最外层函数外面定义的变量拥有全局作用域
- (2)所有末定义直接赋值的变量自动声明为拥有全局作用域
- (3)所有window对象的属性拥有全局作用域
- 和全局作用域相反,局部作用域一般只在固定的代码片段内可访问到,最常见的例如函数内部,所有在一些地方也会看到有人把这种作用域称为函数作用域
- */
- //6.请定义一个函数,该函数功能为,传入一个值,返回这个值的平方值(就是这个值相乘的结果)
- /*
- function getSquare(x){
- return x*x;
- }
- console.log("getSquare(10):"+getSquare(10));*/
- //7.请写出一个函数,功能为:传入三个值,则返回这三个值最大的那个值
- /*function getMax(a,b,c){
- var max = a;
- if(b>a){
- max = b;
- }
- if(c>max){
- max = c;
- }
- return max;
- }
- console.log(getMax(1132,212,1));*/
- //8.请写出一个函数,功能为:传入二个值,分别是x和y,返回x的y次方结果
- /* function getPow(x,y){
- return Math.pow(x,y);
- }
- console.log("getPow:"+getPow(2,10));*/
- //9.请定义一个函数,输入一个N值,则计算出该值的阶乘,即:n! = 1*2*3*4*5*6*...*N
- /* function getFactorial(x){
- if(x==0){
- return 1;
- }
- else{
- return x*getFactorial(x-1);
- }
- }
- console.log("getFactorial:"+getFactorial(5));*/
- //10.模拟一个长按事件(提示,用定时器实现)
- //提示
- /* div.addEventListener("mousedown",function(){
- alert("鼠标左键点下去触发")
- })
- div.addEventListener("mouseup",function(){
- alert("鼠标左键弹上来时触发")
- })
- */
- /*
- document.write("<div><h1>按我</h1></div>");
- var div = document.querySelector("div");
- div.addEventListener("mousedown",function(){
- setTimeout(console.log("鼠标左键点下去触发"),2000);
- })
- div.addEventListener("mouseup",function(){
- setTimeout(console.log("鼠标左键弹上来时触发"),2000);
- })
- */
- //11.模拟一个双击事件(提示,用定时器实现)
- /* function getTime(){
- var date = new Date();
- return date.getTime();
- }
- var t = 0;
- var f1;
- var f2;
- var interval = 300;
- window.addEventListener("click",function(){
- t++;
- if(t==1){
- console.log("1");
- f1 = getTime();
- }
- if(t==2){
- console.log("2");
- f2 =getTime();
- console.log(f2-f1);
- if(f2-f1<interval){
- alert("dbclick!");
- }
- t=0;
- }
- })
- */
- //12.实现一个任意时间的倒计时。(提高题)
- //提示,获取当前的时间戳 (new Date()).getTime()
- /*
- var box = document.querySelector("#box");
- setInterval(setVal,1000);
- var t = "2017,8,25";
- var ts = t.split(",")
- var date2 = new Date(t);
- function setVal(){
- var date1 = new Date();
- var interval = parseInt(parseInt(date2.getTime())-parseInt(date1.getTime()));
- var day = parseInt(interval/1000/60/60/24);
- var hour = parseInt((interval-day*1000*60*60*24)/60/60/1000);
- var minute = parseInt((interval-day*1000*60*60*24-hour*60*60*1000)/60/1000);
- var second = parseInt((interval-day*24*60*60*1000-hour*60*60*1000-minute*60*1000)/(1000)%(60*60));
- var s = "距离"+ts[0]+"年"+ts[1]+"月"+ts[2]+"日"+"还有"+day+"天"+hour+"时"+minute+"分"+second+"秒";
- box.innerHTML = s;
- }*/
- </script>
- </body>
- </html>
Javascript---- 练习五
来源: http://www.bubuko.com/infodetail-2226094.html