码迷,mamicode.com
首页 > Web开发 > 详细

JS学习记录(补充三)

时间:2017-08-04 00:23:02      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:指定   ati   name   res   com   result   端口   count   reload   

函数
<html lang="en"> <head> <meta charset="UTF-8"> <title>函数</title> </head> <body> <button onclick="showName(‘ccy‘)">显示陈传印名字</button> <button onclick="showName(‘lzw‘)">显示我的名字</button> <button onclick="showName(‘zxl‘)">显示小亮的名字</button> <script> /*无参函数*/ /*function showName(){ /!*()包裹参数*!/ alert("lzw"); }*/ /*1:直接调用*/ // showName(); /*()执行*/ /*2:和元素事件绑定*/ /*形式参数:定义函数时指定的参数,具体数据是由实际参数决定 * 实际参数:调用函数的时候指定的参数,实参的值会影响形式参数*/ /*有参函数 在函数中的参数为“形式参数”*/ function showName(name) { alert(name); } </script> </body> </html>

结果图:

技术分享

 

调用有(无)参函数

<html lang="en"> <head> <meta charset="UTF-8"> <title>调用有(无)参函数</title> </head> <body> <input name="bth" type="button" value="显示HelloWord" onclick="showHello(prompt(‘显示HelloWord的次数:‘))"> </body> <script> function showHello(count) { for(i = 1;i <= count; i ++){ document.write("<h2>hello word</h2>"); } } </script> </html>

结果图:

技术分享

匿名函数
<html lang="en"> <head> <meta charset="UTF-8"> <title>匿名函数</title> </head> <body> </body> <script> /*匿名函数用变量:下列中的“show”接收。变量名称+()可以让函数执行*/ var show= function () { alert("1111"); }; show(); /*匿名函数的自调用*/ (function () { alert("我是匿名函数!") })(); </script> </html>

 

结果图:

技术分享技术分享

 

return关键字
<html lang="en"> <head> <meta charset="UTF-8"> <title>return关键字</title> </head> <body> </body> <script> function calc(num1, num2, c) { switch (c) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; case "*": result = num1 * num2; break; case "/": result = num1 / num2; break; } return result; } var r = calc(2, 3, "*"); alert(r); </script> </html>

结果图:

技术分享

 

Arguments:数组对象,用于保存函数的参数

<html lang="en"> <head> <meta charset="UTF-8"> <title>Arguments:数组对象,用于保存函数的参数</title> </head> <body> </body> <script> /*在实参个数不确定的时候,可以省略形参,在函数体内部使用arguments * arguments是一个数组,里面包含了调用函数调用的所有实参!*/ function calc() { var result = 0; var length = arguments.length; if (length == 2) { /*传递的参数为两个*/ result = arguments[0] + arguments[1]; } else if (length == 3) { /*传递的参数为三个*/ switch (arguments[2]) { case "+": result = arguments[0] + arguments[1]; break; case "-": result = arguments[0] - arguments[1]; break; case "*": result = arguments[0] * arguments[1]; break; case "/": result = arguments[0] / arguments[1]; break; } } return result; } // var result2 = calc(10, 6); /*参数有两个时默认为加法*/ var result2 = calc(1, 6, "*"); /*乘法*/ alert(result2); </script> </html>

结果图:

技术分享

 

callee属性
<html lang="en"> <head> <meta charset="UTF-8"> <title>callee属性</title> </head> <body> </body> <script> function show() { /*arguments.callee属性只想函数本身。 * 可以用于递归*/ console.log(arguments.callee); } show("lzw"); /*1+2+3+4+5+6+7+8+9+10*/ var sum = 0; function calc(num) { sum += num; num ++; if (num <= 10){ arguments.callee(num); } } calc(1); alert(sum); </script> </html>

结果图:

技术分享

 

this
<html lang="en"> <head> <meta charset="UTF-8"> <title>this</title> </head> <body> </body> <script> var zhangsan = { name:"zhangsan", age:"26", height:"182", say:function () { alert(zhangsan.name); console.log(this); }, eat:function(){ alert("汉堡!") } }; zhangsan.say(); /*function show() { alert(this); } show();*/ </script> </html>

结果图:技术分享

 

 

程序调试
<html lang="en"> <head> <meta charset="UTF-8"> <title>程序调试</title> </head> <body> </body> <script> /*F10:代码一行一行执行,遇到代码块,一步执行完毕 * F11:代码一行一行执行,遇到代码块,进入代码块内部 * F12:如果代码运行在代码内部,跳出代码块*/ function showHello() { alert("Hello1"); alert("Hello2"); alert("Hello3"); alert("Hello4"); alert("Hello5"); } alert("开始输出Hello"); showHello(); alert("输出Hello结束"); </script> </html>

 

结果图:

先输出“开始输出Hello,之后Hello1~Hello5,最后Hello结束”

 

 

全局,局部变量

<html lang="en"> <head> <meta charset="UTF-8"> <title>全局,局部变量</title> </head> <body> </body> <script> /*全局变量*/ // var num = 10; var num = 5; function calc1() { /*局部变量*/ // var num = 10; /*全局变量:没有用var修饰的变量,会一层一层的往上找。 * 如果找到同名变量,就进行赋值或者是覆盖。 * 如果到最后都没有找到同名变量,就声明一个同名全局变量*/ num = 10; alert(num + 15); } function calc2() { alert(num + 20); } calc1(); calc2(); </script> </html>

 

结果图:

技术分享技术分享

 

Screen

<html lang="en"> <head> <meta charset="UTF-8"> <title>Screen</title> </head> <body> </body> <script> console.log(screen.width); console.log(screen.height); console.log(screen.availWidth); console.log(screen.availHeight); </script> </html>

结果图:

技术分享

 

Location

<html lang="en"> <head> <meta charset="UTF-8"> <title>Location</title> </head> <body> <button onclick="assign()">加载新页面</button> <button onclick="replace()">替换页面</button> <button onclick="reload1()">刷新当前页面</button> <button onclick="reload2()">彻底刷新当前页面</button> </body> <script> function assign() { /*可以返回老页面*/ location.assign("http://www.baidu.com"); } function replace() { /*不能返回老页面*/ location.replace("http://www.baidu.com"); } function reload1() { location.reload(); } function reload2() { location.reload(true); } </script> <!--<script> console.log(location.href);/*完整的url*/ console.log(location.protocol);/*协议*/ console.log(location.port);/*端口号*/ console.log(location.hostname);/*主机名称*/ console.log(location.pathname);/*路径名称*/ console.log(location.search);/*?后的数据部分*/ </script>--> </html>

结果图:

技术分享

 

JS学习记录(补充三)

标签:指定   ati   name   res   com   result   端口   count   reload   

原文地址:http://www.cnblogs.com/lizuowei/p/7282678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!