Javascript 笔记 DAY7

来源:互联网 发布:韩国81端口活跃ip段 编辑:程序博客网 时间:2024/05/22 14:55
//with语句 将代码的作用域设置到一个特定对象中。定义with主要是
//为了简化多次编写同一个对象的工作 例如:


// var qs = location.search.substring(1);
// var hostname = location.hostname;
// var url = location.href;
// //用with
// with(location){
// var qs = search.substring(1);
// var hostname = hostname;
// var url = href;
// }
//大量使用with会导致性能下降,并且不易调试,大型项目不推荐使用with


//switch case, js中switch(express)括号中的表达式可以是任何数据类型
//不像有的语言只能用整形表达式
// switch(indx){


// case 1: alert("1");
// break;
// default:
// alert("都不满足默认");
// } 




//还可以这样使用
// var num =20;
// switch(true){
//   case num<20: alert("budui");
//   break;
//   case num>=20:alert("right");
//   break;
//   default:
//   alert("没有答案");
// }


//switch(ex)语句在比较值时是使用全等操作符,因此不会发生类型
//z转换,例如"10" 不等于数值10




//函数
//执行return语句之后会立即停止并退出,return之后的代码不会执行
// function getTest(){
//   return 1+1;
//   alert("ss")//不会执行


// }
// alert(getTest());


//return 也可以不返回值 直接return;表示结束跳出函数。默认返回的值是undefined
//


//JS中函数不能重载,两个函数重载只会临近原则调用最近那个
//js中函数虽然你定义了参数也可以不传或者多传都没问题
//因为js中传递参数是一个数组[] 它不管你数组有没元素,
// 函数数组arguments
// function test(a){
// alert(1);
// }


// function test(){
// alert(2);
// }
// test(1,2);//无论传不传参数都是调用最近的test()函数,始终弹出2




// function test(){


// alert(arguments[0]+arguments[1]);
// }


// test(1,"2"); //弹出12


//虽然JS函数没有重载,我们可以通过判断其参数个数也能达到相同效果,例如:
// function test(){
// if(arguments.length==1){
// alert(arguments[0]);
// }else if(arguments.length==2){
// alert("我是重载"+arguments[0]+":"+arguments[1]);
// }


// }


// test(0);
// test(1,2);
0 0
原创粉丝点击